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

问题描述 投票:0回答:1

我的应用程序中有一个使用React Native Router Flux的tabbar。有几个用例,根据当前用户隐藏或显示特定选项卡会非常有用。我遇到的主要问题是:

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

react-native-router-flux库不支持从我看到的任何选项。我该如何实现此功能?

react-native react-native-navigation react-native-router-flux
1个回答
3
投票

react-native-router-flux中的默认tabbar组件只是react-navigation-tabs库中的组件。您可以将此组件直接导入到代码中,根据需要进行自定义,然后通过react-native-router-flux prop(tabBarComponent)将其传递给documented here

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

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)

然后只需在我的Tabs组件中导入并使用它:

<Tabs
  key="main"
  tabBarComponent={TabBarComponent} // the component defined above
  ...

详细了解这些东西传递到哪里

查看react-native-router-flux的the line of the source,它使用来自createBottomTabNavigator库的react-navigation,如果不传递自定义tabBarComponent则不传递任何组件。 createBottomTabNavigator中的react-navigation方法来自from this line of the library,实际上是在react-navigation-tabs中定义的。现在,我们可以herereact-navigation-tabs中看到,如果没有传递tabBarComponent,它只使用BottomTabBar,也在react-navigation-tabs中定义。这个BottomTabBar反过来,takes a custom tab button renderer through props,称为getButtonComponent

© www.soinside.com 2019 - 2024. All rights reserved.