【问题标题】:React Navigation 4.x tabBarOptions not working TypescriptReact Navigation 4.x tabBarOptions 不工作打字稿
【发布时间】:2020-07-21 08:42:25
【问题描述】:

请不要推荐我使用 React Navigation 5.x,反正我使用 4.x 因为 switchNavigator。在将项目迁移到 typescript 后,我​​遇到了 React Navigation 4.x 的问题,这是出现错误的代码的一部分。

 const MainTabs = createBottomTabNavigator({
  Home: {
    screen: HomeStack,
    navigationOptions: {
      tabBarIcon: ({focused}) => {
        const image = focused ? Images.inicio_onFocus : Images.inicio_onBlur;
        return <Image source={image} />;
      },
      tabBarOptions: {
        activeTintColor: colors.active,
        inactiveTintColor: colors.background,
        style: {
          marginTop: 10,
          height: 65,
          backgroundColor: colors.title,
          shadowColor: colors.inactive,
          borderTopColor: 'transparent',
          borderTopWidth: 0,
          elevation: 5,
          shadowOpacity: 5,
          shadowRadius: 10,
        },
      },
      tabBarLabel: 'INICIO',
    },
  },
  Vacations: {
    screen: VacationsStack,
    navigationOptions: {
      tabBarIcon: ({focused}) => {
        const image = focused ? Images.vacaciones_onFocus : Images.vacaciones_onBlur;
        return <Image source={image} />;
      },
      /*
      tabBarOptions: {
        activeTintColor: colors.active,
        inactiveTintColor: colors.background,
        style: {
          marginTop: 10,
          height: 65,
          backgroundColor: colors.title,
          shadowColor: colors.inactive,
          borderTopColor: 'transparent',
          borderTopWidth: 0,
          elevation: 5,
          shadowOpacity: 5,
          shadowRadius: 10,
        },
      },
      */
      tabBarLabel: 'VACACIONES',
    },
  },
  HoursReport: {
    screen: HoursReportStack,
    navigationOptions: {
      tabBarIcon: ({focused}) => {
        const image = focused ? Images.imputar_onFocus: Images.imputar_onBlur;
        return <Image source={image} />;
      },
      /*
      tabBarOptions: {
        activeTintColor: colors.active,
        inactiveTintColor: colors.background,
        style: {
          marginTop: 10,
          height: 65,
          backgroundColor: colors.title,
          shadowColor: colors.inactive,
          borderTopColor: 'transparent',
          borderTopWidth: 0,
          elevation: 5,
          shadowOpacity: 5,
          shadowRadius: 10,
        },
      },
      */
      tabBarLabel: 'IMPUTAR',
    },
  },
  ExpenseReport: {
    screen: ExpenseReportStack,
    navigationOptions: {
      tabBarIcon: ({focused}) => {
        const image = focused ? Images.rendir_onFocus : Images.rendir_onBlur;
        return <Image source={image} />;
      },
      /*
      tabBarOptions: {
        activeTintColor: colors.active,
        inactiveTintColor: colors.background,
        style: {
          marginTop: 10,
          height: 65,
          backgroundColor: colors.title,
          shadowColor: colors.inactive,
          borderTopColor: 'transparent',
          borderTopWidth: 0,
          elevation: 5,
          shadowOpacity: 5,
          shadowRadius: 10,
        },
      },
      */
      tabBarLabel: 'RENDIR',
    },
  },
});

包含 tabBarOptions 的代码的每一部分都出现此错误。

The expected type comes from property 'navigationOptions' which is declared here on type 'NavigationRouteConfig<NavigationBottomTabOptions, NavigationTabProp<NavigationRoute<NavigationParams>, any>, unknown>'

【问题讨论】:

    标签: typescript react-native react-navigation


    【解决方案1】:

    看来tabBarOptions 放错地方了,也许这种方式适合你:

    tabBarOptions 不是navigationOptions 的属性。它应该放在createBottomTabNavigator 的第二个参数中,就像initialRouteName 一样。

    const MainTabs = createBottomTabNavigator({
      Home: {
        screen: HomeStack,
        navigationOptions: {
          tabBarIcon: ({focused}) => {
            const image = focused ? Images.inicio_onFocus : Images.inicio_onBlur;
            return <Image source={image} />;
          },
          tabBarLabel: 'INICIO',
        },
      },
      Vacations: {
        screen: VacationsStack,
        navigationOptions: {
          tabBarIcon: ({focused}) => {
            const image = focused ? Images.vacaciones_onFocus : Images.vacaciones_onBlur;
            return <Image source={image} />;
          },
          tabBarLabel: 'VACACIONES',
        },
      },
      HoursReport: {
        screen: HoursReportStack,
        navigationOptions: {
          tabBarIcon: ({focused}) => {
            const image = focused ? Images.imputar_onFocus: Images.imputar_onBlur;
            return <Image source={image} />;
          },
          tabBarLabel: 'IMPUTAR',
        },
      },
      ExpenseReport: {
        screen: ExpenseReportStack,
        navigationOptions: {
          tabBarIcon: ({focused}) => {
            const image = focused ? Images.rendir_onFocus : Images.rendir_onBlur;
            return <Image source={image} />;
          },
          tabBarLabel: 'RENDIR',
        },
      },
    }, {
      tabBarOptions: {
        activeTintColor: colors.active,
        inactiveTintColor: colors.background,
        style: {
          marginTop: 10,
          height: 65,
          backgroundColor: colors.title,
          shadowColor: colors.inactive,
          borderTopColor: 'transparent',
          borderTopWidth: 0,
          elevation: 5,
          shadowOpacity: 5,
          shadowRadius: 10,
        }
      }
    }});
    

    【讨论】:

    • 嗯......这很有效,很奇怪,因为在我以前的项目(javascript)中,我有这个方式,无论如何,非常感谢
    • 很高兴我能帮上忙! :) 是的,库 react-navigation 非常频繁地更改这些属性的位置。幸运的是,我找到了一个个人项目(也在 v4.x.x 上运行)并为您写下解决方案。
    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2022-11-09
    • 2019-04-20
    • 2019-03-04
    • 2013-08-11
    相关资源
    最近更新 更多