如何通过react-native-router -flux在react-native中更新(上一个)屏幕

时间:2018-10-05 11:23:21

标签: react-native react-native-router-flux

我在应用程序中使用react-native-router-flux。

-我在第一个屏幕上使用参数,然后导航到第二个屏幕。

-在第二个屏幕上,我正在更新在第一个屏幕上使用的参数。

-我希望在我回到该屏幕(第一个屏幕)时进行更新。

如何实现此功能。

谢谢

2 个答案:

答案 0 :(得分:0)

@Vinzzz是正确的最佳解决方案是使用redux。但是,在这里,如何在没有它的情况下可以使它工作。

导航到另一个视图时,您将调用动作。OTHER_VIEW()

您可以将参数传递给此功能,该功能将通过与您的按键对应的组件中的props可用:“ OTHER_VIEW”

这里有个例子:

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
}

答案 1 :(得分:0)

我不使用Redux即可解决此问题。

将Screen First的api调用语句放在一个方法中(在First Screen上),并将其传递到第二个屏幕。在第二个屏幕上,我在componentWillUnmount()内部调用了此方法。它会更新我的第一个屏幕,因为我将从第二个屏幕返回。