Refresh previous screen on goBack()

前端 未结 11 1837
天命终不由人
天命终不由人 2020-12-13 09:34

I am new to React Native. How can we refresh/reload previous screen when returning to it by calling goBack()?

Lets say we have 3 screens A, B, C:

<
11条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 10:16

    This answer assumes that the react-native-navigation library is being used, which is unlikely because it doesn't actually have a goBack() method...

    The constructor doesn't call a second time because screen A and B are still rendered (but hidden behind screen C). If you need to know when screen B is going to be visible again you can listen to navigation events.

    class ScreenB extends Component {
      constructor(props) {
        super(props);
        // Listen to all events for screen B
        this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
      }
    
      onNavigatorEvent = event => {
        switch (event.id) {
          case 'willAppear':
            // refresh your state...
            break;
      };
    }
    

    Other events: willDisappear, didAppear, didDisappear

    An alternate solution to your problem is to use a state management solution like Redux to provide the state to all screens whenever it is updated (rather than just on screen transitions. See old react-native-nav/redux example.

提交回复
热议问题