如何在defaultNavigationOptions上添加订阅

时间:2019-03-15 03:04:37

标签: react-native react-navigation

我遇到过这个solution,试图订阅导航生命周期的更新

  

Navigator.js

const App = createStackNavigator(
  {
    screen1: { screen: MainScreen },
    screen2: { screen: SecondaryScreen },
  },
  {
    cardStyle: {
      backgroundColor: 'transparent'
    }
  }
);

const MainNavigator = createAppContainer(App);

export default MainNavigator;

我想知道是否可以订阅此navigator文件中的事件吗?还是我别无选择,只能在每个屏幕上进行订阅?

1 个答案:

答案 0 :(得分:0)

您可以像这样为每个屏幕添加侦听器

constructor(props) {
        super(props);
        this.props.navigation.addListener(
            'willFocus',//event name
            payload => {
                //do your work here
            }
        );
    }

其他事件名称'onDidFocus', 'onWillBlur' ,'onDidBlur'

reference