在 React Native Router Flux Tabbar 中以编程方式隐藏和显示各个选项卡

Rob*_*wal 1 react-native react-native-router-flux react-native-navigation

我的应用程序中有一个使用 React Native Router Flux 的标签栏。在一些用例中,基于当前用户隐藏或显示特定选项卡会非常有帮助。我遇到的主要是:

  • AB 向特定用户测试新标签
  • 向具有特定权限的特定用户显示特殊的管理选项卡

从我所见,react-native-router-flux 库不支持任何选项来执行此操作。我怎样才能实现这个功能?

Rob*_*wal 5

react-native-router-flux 中的默认 tabbar 组件只是react-navigation-tabs库中的组件。您可以根据需要直接输入该组件到你的代码,自定义,然后把它传递给react-native-router-flux通过tabBarComponent道具(这里记录)。

我创建了一个新组件,您应该能够直接复制它,只需根据您的状态更改实际隐藏选项卡的逻辑:

import React from 'react'
import { BottomTabBar } from 'react-navigation-tabs'
import { View, TouchableWithoutFeedback } from 'react-native'
import { connect } from 'react-redux'

const HiddenView = () => <View style={{ display: 'none' }} />
const TouchableWithoutFeedbackWrapper = ({
  onPress,
  onLongPress,
  testID,
  accessibilityLabel,
  ...props
}) => (
  <TouchableWithoutFeedback
    onPress={onPress}
    onLongPress={onLongPress}
    testID={testID}
    hitSlop={{
      left: 15,
      right: 15,
      top: 5,
      bottom: 5,
    }}
    accessibilityLabel={accessibilityLabel}
  >
    <View {...props} />
  </TouchableWithoutFeedback>
)
const TabBarComponent = props => (
  <BottomTabBar
    {...props}
    getButtonComponent={({ route }) => {
      if (
        (route.key === 'newTab' && !props.showNewTab) ||
        (route.key === 'oldTab' && props.hideOldTab)
      ) {
        return HiddenView
      }
      return TouchableWithoutFeedbackWrapper
    }}
  />
)

export default connect(
  state => ({ /* state that you need */ }),
  {},
)(TabBarComponent)
Run Code Online (Sandbox Code Playgroud)

然后简单地导入并在我的 Tabs 组件中使用它:

<Tabs
  key="main"
  tabBarComponent={TabBarComponent} // the component defined above
  ...
Run Code Online (Sandbox Code Playgroud)

详细查看这些东西被传递到哪里

查看react-native-router-flux的源代码行,它是createBottomTabNavigatorreact-navigation库中使用的,如果不传递自定义 tabBarComponent,则不传递任何组件。createBottomTabNavigatorin的方法react-navigation来自库的这一行,实际上是在react-navigation-tabs. 现在,我们可以在这里看到react-navigation-tabs,如果没有传递 tabBarComponent,它只会使用同样在react-navigation-tabs. 这BottomTabBar反过来又通过 props 获取自定义选项卡按钮渲染器,称为getButtonComponent.