How to update back(previous) screen in react-native by react-native-router -flux

主宰稳场 提交于 2021-02-11 13:26:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!