为什么 React 导航事件“willFocus”不执行?

Aru*_*mar 4 react-native react-navigation

我在 React Native 应用程序中使用 React-navigation v2 创建了一些选项卡。我创建了 componentDidMount 函数,其中调用 willFocus。第一次运行应用程序并且第一次选择我想要的选项卡时,willFocus 不会执行。但是,当我转到不同的选项卡并返回该选项卡时,现在 willFocus 会执行。willFocus第一次不执行而第二次运行正常的原因是什么?

所需的选项卡 componentDidMount 函数

componentDidMount(){
    const {navigation} = this.props;
    navigation.addListener ('willFocus', async () =>{
      console.log("willFocus runs"
    });
}
Run Code Online (Sandbox Code Playgroud)

选项卡导航器与其他导航器嵌套,但我只显示下面的选项卡导航器

TabNav: createBottomTabNavigator(
        {
            TAB1: Screen1,
            TAB2: Screen2,
            TAB3: Screen3,
            TAB4: Screen4,
            TAB5: Screen5,
            // Map: MapScreen
        },
        {
            initialRouteName: 'Bar',
            transitionConfig: NavigationConfig,
            navigationOptions: ({navigation})=>({
                tabBarIcon: ({focused, tintColor})=>{
                    const { routeName } = navigation.state;
                    let iconName, iconSize;
                    switch(routeName) {
                                case "TAB1":
                                    iconName = `icon1`;
                                    break;
                                case "TAB2":
                                    iconName = `icon2`;
                                    break;
                                case "TAB3":
                                    iconName = `icon3`;
                                    break;
                                case "TAB4":
                                    iconName = `icon4`;
                                    break;
                                case "TAB5":
                                    iconName = `icon5`;
                                    break;
                                default:
                                    break;
                                }
                    return <Icon name={iconName} color={tintColor} type="FontAwesome" />;
                },
            }),
            tabBarOptions: {
                activeTintColor: 'black',
                inactiveTintColor: 'grey',
                activeBackgroundColor: '#abaf9b',
                labelStyle: {
                    fontSize: 12,
                },
                // style for tabbar
                style: {
                    backgroundColor: '#ffffff',
                    height: 60,
                    justifyContent: 'space-around',
                    alignContent: 'center',
                    alignItems: 'center',
                },
                // style for tab
                tabStyle: {
                    paddingTop: 7,
                    paddingBottom: 7
                }
            },
        }
    ),
Run Code Online (Sandbox Code Playgroud)

sin*_*nan 8

我也遇到了这个问题,它被报告为反应导航的错误。查看问题了解详细信息。

我有两个建议来解决这个问题。

  1. 尝试更新react-navigation并检查问题是否解决
  2. 如果第一个解决方案不起作用,作为解决方法,如果您想确保在应用程序初始启动和屏幕更改时都调用该函数,我将调用该函数两次。

PS:再次检查您的函数并确保调用该函数两次不会导致任何副作用

componentDidMount(){
    console.log("willFocus runs") // calling it here to make sure it is logged at initial start

    const {navigation} = this.props;
    navigation.addListener ('willFocus', async () =>{
      console.log("willFocus runs") // calling it here to make sure it is logged at every time screen is focused after initial start
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 目前我正在使用相同的方法,但缺点是如果有 api 调用,那么它会不必要地调用 2 次 (4认同)
  • "..navigation.addListener ('willFocus', **async** () =&gt;{" ...这是我缺少的部分!我必须在侦听器中使用“async”!非常感谢 (2认同)