4

我正在编程 React Native App。我正在使用 react-navigation 来导航屏幕。

我有 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()}/>
    )
  }
}

我想将来自 TabClass 组件的 clickButton() 函数传递给处理事件单击标签栏的代码。当 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,我点击 User Tab Item,这个函数会运行;如果我停留在 HomeTab,我点击 Home Tab Item,这个函数会不跑)。

有什么方法可以处理选项卡项目上的点击事件。

4

3 回答 3

2

react-navigation 4.x 文档tabBarOnPress中,您可以在以下范围内覆盖navigationOptions

tabBarOnPress: ({ navigation, defaultHandler }) => {
  defaultHandler(); // Call the default handler to actually switch tabs
  // do extra stuff here
},
于 2020-01-18T18:08:54.580 回答
2

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

于 2018-05-24T17:23:13.473 回答
1

自定义 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',
        });
于 2018-01-16T08:15:29.183 回答