反应本机导航 v5 标签按不起作用

React native navigation v5 tab press not working

如代码所示,未调用 tabPress,是我做错了还是遗漏了什么,不幸的是我还没有找到反应导航版本 5 的任何代码示例。

<Tab.Navigator labeled={false} barStyle={{backgroundColor: '#ffffff', height: 55}} options={{
        tabPress: ({navigation}) => {
            console.log('nav tab press triggered')
        }
    }}>
        <Tab.Screen name={`DeviceNavigatorTab`} component={DeviceNavigator} options={{
            tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_home-menu.png')}
                                                style={{width: 26, height: 26, tintColor}}/>,
            tabPress: ({navigation}) => {
                console.log('tab press triggered')
            }
        }} tabPress={() => { console.log('prop tab pressed') }}/>
        <Tab.Screen name={`AlarmNavigatorTab`} component={AlarmNavigator} options={{
            tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_alert-circle.png')}
                                                style={{width: 26, height: 26, tintColor}}/>,
        }}/>
        <Tab.Screen name={`ProfileNavigatorTab`} component={ProfileNavigator} options={{
            tabBarIcon: ({tintColor}) => <Image source={require('../../images/feather_user.png')}
                                                style={{width: 26, height: 26, tintColor}}/>,
        }} />
    </Tab.Navigator>

您需要在您的组件中 listen/subscribe "tabPress" 事件。

React.useEffect(() => {
  const unsubscribe = navigation.addListener('tabPress', e => {
    // Prevent default behavior
    e.preventDefault();

    // Do something manually
    // ...
  });

  return unsubscribe;
}, [navigation]);

我在文档中发现了一些我以前从未见过的新内容,这是将侦听器添加到屏幕的方法,每次用户单击选项卡时,它都会转到此选项卡内的第一个堆栈屏幕

https://reactnavigation.org/docs/navigation-events/#listeners-prop-on-screen

<Tab.Screen 
  name="DeviceNavigatorTab" 
  component={DeviceNavigator} 
  listeners={({ navigation, route }) => ({
    tabPress: e => {
      if (route.state && route.state.routeNames.length > 0) {
          navigation.navigate('Device')
      }
    },
  })}
/>