I used to have an app built on NavigatorIOS, and I use passProps to communicate for parent-children communication.
I am now switching to Navigator but it seems that passProps is no longer effective. What's the good pattern to pass properties in Navigator?
Many thanks for your help.
Hey macelii, thanks for reporting this issue!
React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.
react-native or for more real time interactions, ask on Discord in the #react-native channel.You can use renderScene of navigator which gives you access to the route and navigator objects.
Then when you want to push a new scene you can pass the properties and have them accessible inside the route object.
Example of the renderScene function:
renderScene(route, navigator) {
switch (route.id) {
case 'home':
return <Home navigator={navigator} data={route.data} />;
}
}
Example of the pushing a new scene:
this.props.navigator.push({
id: 'home',
data: Object
})
Thank you so much for looking @christopherdro!
@mkonicek No Problem! :)
@macelii Going to close this issue out for now since this questions seems more appropriate for Stack Overflow. Feel free to post your question there or join us on Discord if you'd like some more help.
Hope you get it figured out! :)
Thanks @christopherdro , this help many
In addition to the above, if you want to pass a bunch of props you can do this:
this.props.navigator.push({
id: viewId,
props: {
someProp: this.props.someProp
}
});
and in your navigator's parent:
renderScene(route, navigator) {
switch (route.id) {
case 'home':
return <Home navigator={navigator} {... route.props} />;
}
}
@coconup, and we only need to remember that: Home will copy all route.props as its own props.
So , to use those route.props within Home, e.g someProp, just access via this.props.someProp.
I alway fount issue in redirection "Cannot read property 'push' of undefined". what is making wrong ?
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,Navigator,
Text,TouchableOpacity,
View
} from 'react-native';
export default class Main extends Component {
changepage(a){
alert("Page name: "+a);
this.props.navigator.push({
id: a,
})
}
render() { } renderScene(route, navigator) { const styles = StyleSheet.create({
return (
<TouchableOpacity >
<Text style={styles.welcome} onPress={this.changepage('Test')}>
Login please
</Text>
</TouchableOpacity>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
</View>
);
switch (route.id) {
case 'Test':
return
}
}
}
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('Main', () => Main);
really awesome! @christopherdro. your advice helped me a lot
@christopherdro! I am trying your suggestion. But renderScene never gets called in my case. Any idea what I might be missing ?
Most helpful comment
You can use
renderSceneof navigator which gives you access to therouteandnavigatorobjects.Then when you want to push a new scene you can pass the properties and have them accessible inside the route object.
Example of the renderScene function:
Example of the pushing a new scene: