首页 > 解决方案 > React Native:在切换底部导航选项卡时设置参数

问题描述

我在我的一个应用程序中使用底部导航,但遇到了问题。

每当用户使用底部导航登陆页面时,我想向云端发送一些事件,比如 A1。

这是我的代码:

const tabBarConfigs = {
  lazy: true,
  animationEnabled: true,
  tabBarComponent: props => <AppBottomTab {...props} />,
  tabBarOptions: {
    style: {
      backgroundColor: Fonts.Colors.transparent,
    },
    activeTintColor: Fonts.Colors.reddishPink,
    inactiveTintColor: Fonts.Colors.fullBlack,
    labelStyle: { fontFamily: Fonts.Medium, fontSize: 10, marginTop: -2 },
    upperCaseLabel: true,
    allowFontScaling: false,
    showIcon: true,
  },
  defaultNavigationOptions: {
    tabBarOnPress: ({ navigation, defaultHandler }) => {
      defaultHandler();
      navigation.setParams({
        eventData: {
          source: "bottom_nav",
        },
      });
      if (navigation.isFocused()) {
        const scrollToTop = navigation.getParam("scrollToTop", null);
        if (scrollToTop) {
          scrollToTop();
        }
      }
    },
  },
};

问题是,当用户从任何其他接触点(底部导航除外)着陆时,导航中也已经设置了 eventData。如何避免这种情况?

已经为此苦苦挣扎了3天。任何人都可以在这里帮忙吗?

标签: javascriptandroidiosreactjsreact-native

解决方案


推荐阅读