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

时间:2017-08-04 09:57:44

标签: react-native navigation react-navigation tabnavigator ex-navigation

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

我有2个Stack Navigators,它们留在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,这个功能不会运行。)

有没有办法处理标签项上的点击事件。

2 个答案:

答案 0 :(得分: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',
        });

答案 1 :(得分:0)

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