如何使用 react-navigation 处理 React Native App 中 TabNavigator 选项卡项的点击事件?

How to handle click event on tab item of TabNavigator in React Native App using react-navigation?

我正在编写 React Native App。我正在使用反应导航来导航屏幕。

我有 2 个 Stack Navigator,它们都位于 Tab Navigator 中。当我按下 TabBar 上的选项卡项以刷新其视图时,我想处理点击事件。

export const HomeStack = StackNavigator({
  Home: {screen: ScreenHome},
  Detail: {screen: ScreenDetail}
})
export const UserStack = StackNavigator({
  User: {screen: ScreenUser}
})
export const Tabbar = TabNavigator({
  HomeTab: {
    screen: HomeStack,
  },
  UserTab: {
    screen: UserStack,
  }   
}, {
  tabBarComponent: ({ jumpToIndex, ...props}) => (
       <TabBarBottom
          {...props}
          jumpToIndex={(index) => {
            if(props.navigation.state.index === index) {
            props.navigation.clickButton(); //----> pass props params (code processes)
          }
          else {
            jumpToIndex(index);
          }
        }
      }
    />
   ),
   tabBarPosition: 'bottom'
});
class TabClass extends Component{
  constructor(props){
    super(props)
    this.clickButton = this.clickButton.bind(this)
  }
  clickButton(){
    console.log('click button')
  }
  render (){
    return (
      <Tabbar  clickButton={()=> this.clickButton()}/>
    )
  }
}

我想将 clickButton() 函数从 TabClass 组件传递给处理事件点击标签栏的代码。当 if(props.navigation.state.index === index) 时,我希望它会调用 clickButton()。我试过了,但没用。 有什么办法可以解决我的问题吗?

我尝试了 onNavigationStateChange(prevState, currentState)。

class TabClass extends Component{
  constructor(props){
    super(props)
    this.clickButton = this.clickButton.bind(this)
  }
  clickButton(){
    console.log('click button')
  }
  _handleNavagationStateChange(prevState, currentState){
    console.log('handle navigation state change')
  }
  render (){
    return (
        <Tabbar onNavigationStateChange={(prevState, currentState) => {
          this._handleNavagationStateChange(prevState, currentState)
        }}
        clickButton={()=> this.clickButton()}
        />
    )
  }
}

然而,_handleNavagationStateChange(prevState, currentState) 仅在导航状态改变时运行(例如,如果我停留在 HomeTab,我点击用户选项卡项目,此函数将 运行;如果我停留在HomeTab,我点击Home Tab Item,这个功能不会运行).

有什么方法可以处理标签项上的点击事件。

自定义TabBar的触摸事件请尝试如下代码:

 import { TabBarBottom, TabNavigator, StackNavigator } from 'react-navigation';

 export const MainScreenNavigator = TabNavigator({
    Home: { screen: HomeViewContainer },
    Search: { screen: SearchViewContainer },
 }, {
   tabBarComponent: ({ jumpToIndex, ...props, navigation }) => (
     <TabBarBottom
      {...props}
             jumpToIndex = { tabIndex => {
             const currentIndex = navigation.state.index;

             if (tabIndex == 1) {
             // do some thing. Call Native Live Stream Record
             } else {
                jumpToIndex(tabIndex);
             }
             console.log('Select tab: ', tabIndex);
         }}
        />),
        tabBarPosition: 'bottom',
        });

根据最新文档 here,您可以使用 navigationOptions: {navigationOptions: () => doWhatever()} 来处理标签栏点击。

react-navigation 4.x docs 开始,您可以在 navigationOptions:

中覆盖 tabBarOnPress
tabBarOnPress: ({ navigation, defaultHandler }) => {
  defaultHandler(); // Call the default handler to actually switch tabs
  // do extra stuff here
},