如何在tabnavigator中使用tabBarOnPress反应本机

Swa*_*hal 5 react-native

当我单击选项卡时,我陷入了一个大问题,即我想要onPress事件。我的代码在这里:

static navigationOptions = ({navigation, screenProps}) => {
   const params = navigation.state.params || {};
   console.log("Params:-",params);
  return {
        title:Strings.title_dashboard,
        headerStyle:{ backgroundColor: Colors.header_blue},
        headerTitleStyle:HeaderStyle.titleCenter,
        tabBarOnPress: (tab, jumpToIndex) => {
           console.log("Tab Click",tab);
            jumpToIndex(tab.index);
            navigation.state.params.onFocus()
        },
       headerRight:<TouchableOpacity onPress={()=>Alert.alert(Strings.avanza,Strings.under_imple)}><View><Image source={{uri: "filter_icon"}} style={{height: 18, width: 18,marginRight:10,}} /></View></TouchableOpacity>,
  }
  }
Run Code Online (Sandbox Code Playgroud)

在这里,我在componentDidMount中设置如下参数:

this.props.navigation.setParams({
      handleGrid: this.callServer.bind(this)
    })
Run Code Online (Sandbox Code Playgroud)

在这里出现错误,无法获得此点击事件。

请帮帮我。

谢谢。

Vla*_*sov 12

这是我的方法。它适用于react-navigation 3.xx版本:

let Tabs = createBottomTabNavigator(
  {
    FooTab: Foo,
  },
  {
    initialRouteName: "FooTab",
    defaultNavigationOptions: ({ navigation }) => ({
      tabBarOnPress: ({ navigation, defaultHandler }) => {
        console.log('onPress:', navigation.state.routeName);
        defaultHandler()
      },
    }),
  }
);
Run Code Online (Sandbox Code Playgroud)

对于2.xx版,请使用navigationOptions代替defaultNavigationOptions


小智 4

这对我有用,

static navigationOptions = ({ navigation }) => {
       return {
            tabBarOnPress: ({previousScene, scene, jumpToIndex}) => {
                const { route, index, focused} = scene;

                if(focused){
                    navigation.state.params.scrollToTop()
                }
                jumpToIndex(0)
            }
        }
 };
Run Code Online (Sandbox Code Playgroud)