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

icnyk63a  于 2023-06-30  发布在  React
关注(0)|答案(4)|浏览(235)

我正在开发React Native App。我正在使用react-navigation来浏览屏幕。
我有2个堆栈导航器,他们留在一个标签导航器。我想处理点击事件时,我按下标签栏上的选项卡项目刷新其视图。

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,我点击User Tab Item,这个函数会运行;如果我停留在“主页选项卡”,单击“主页选项卡项”,则此功能将不会运行)。
有什么方法可以处理标签上的点击事件吗?

093gszye

093gszye1#

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

zf2sa74q

zf2sa74q2#

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

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

laik7k3q3#

自定义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',
        });
h9a6wy2h

h9a6wy2h4#

v6的更新答案:

要在Tab键上执行操作(除了内置导航之外),您可以通过screenListeners在导航器上设置tabPress事件监听器,或通过listeners在屏幕上设置tabPress事件监听器。

对于我们的应用程序,以下设置起作用,其中为tabPress事件设置screenListener,然后调用analyticsEvent函数:

const Tab = createBottomTabNavigator<RootTabParamList>();

  return (
      <Tab.Navigator
        initialRouteName={Screens.Home}
        screenListeners={({route}) => ({ // listener receives navigation, route
          tabPress: () => {
            analyticsEvent(route);
          },
        })}>
        // etc.
    )

导航照常进行,但也会触发一个分析事件。
请参阅"Navigation Events" section of the docs以了解更多详细信息。
(根据文档,您还可以通过listeners prop在<Tab.Screen>级别上设置此属性,但这对我不起作用,尽管我没有调试太多。

相关问题