问题
I am using react-native-router-flux in my application.
-I am using a parameter at my first screen and then i navigate to second screen.
-On the second screen i am updating that parameter which was used at my first screen.
-I want it to be updated when i navigate back to that screen (first screen).
How can i implement this functionality.
Thank You
回答1:
@Vinzzz is right best solution is to use redux. But here how you can make this work without it.
When you navigate to another view you call action.OTHER_VIEW()
You can pass parameters to this function, which will be available via props in the component corresponding to your key : "OTHER_VIEW"
Here an example :
routeA.js
constructor(props){
super(props);
var _data = this.props.dataFromRouteB || "";
this.state = {
data : _data
};
}
onPress(){
action.ROUTE_B({dataFromRouteA: this.state.data}); // navigate to route B
}
render(){
const {data} = {...this.state};
return(
<View>
<Text>{data}</Text>
<Button title="go to route B" onPress={()=> this.onPress()} />
</View>
);
}
routeB.js
componentDidMount(){
var a = this.props.dataFromRouteA; // a = ""
a = "hello";
action.ROUTE_A({dataFromRouteB:a}); // navigate to ROUTE_A , data will be available via props
}
回答2:
I Solved this Issue without using Redux.
Put the api calling statement of Screen First in a method(on First Screen) and passed it to second screen. On the second screen i called this method inside componentWillUnmount(). Its updating my first screen as i will be going back from screen second.
来源:https://stackoverflow.com/questions/52664551/how-to-update-backprevious-screen-in-react-native-by-react-native-router-flux