以编程方式隐藏和显示 React Native Router Flux Tabbar 中的各个选项卡
Programatically hiding and showing individual tabs in React Native Router Flux Tabbar
我的应用程序中有一个选项卡栏使用 React Native Router Flux。在一些用例中,根据当前用户隐藏或显示特定选项卡会非常有帮助。我 运行 主要关注的是:
- AB 测试特定用户的新标签
- 向具有特定权限的特定用户显示特殊的管理选项卡
据我所知,react-native-router-flux 库不支持执行此操作的任何选项。我怎样才能实现这个功能?
react-native-router-flux 中默认的 tabbar 组件就是 react-navigation-tabs
库中的组件。您可以将此组件直接导入到您的代码中,根据需要进行自定义,然后通过 tabBarComponent
prop (documented here).
将其传递给 react-native-router-flux
我创建了一个新组件,您应该可以直接复制它并根据您的状态更改实际隐藏选项卡的逻辑:
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
key="main"
tabBarComponent={TabBarComponent} // the component defined above
...
详细看看这些东西被传递到哪里
正在看the line of the source of react-native-router-flux, it is using createBottomTabNavigator
from the react-navigation
library, and passing no component if you do not pass a custom tabBarComponent. The createBottomTabNavigator
method in react-navigation
comes from this line of the library, and is actually defined in react-navigation-tabs
. Now, we can here see in react-navigation-tabs
that if no tabBarComponent has been passed, it simply uses BottomTabBar, also defined in react-navigation-tabs
. This BottomTabBar
, in turn, takes a custom tab button renderer through props,叫getButtonComponent
。
我的应用程序中有一个选项卡栏使用 React Native Router Flux。在一些用例中,根据当前用户隐藏或显示特定选项卡会非常有帮助。我 运行 主要关注的是:
- AB 测试特定用户的新标签
- 向具有特定权限的特定用户显示特殊的管理选项卡
据我所知,react-native-router-flux 库不支持执行此操作的任何选项。我怎样才能实现这个功能?
react-native-router-flux 中默认的 tabbar 组件就是 react-navigation-tabs
库中的组件。您可以将此组件直接导入到您的代码中,根据需要进行自定义,然后通过 tabBarComponent
prop (documented here).
react-native-router-flux
我创建了一个新组件,您应该可以直接复制它并根据您的状态更改实际隐藏选项卡的逻辑:
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
key="main"
tabBarComponent={TabBarComponent} // the component defined above
...
详细看看这些东西被传递到哪里
正在看the line of the source of react-native-router-flux, it is using createBottomTabNavigator
from the react-navigation
library, and passing no component if you do not pass a custom tabBarComponent. The createBottomTabNavigator
method in react-navigation
comes from this line of the library, and is actually defined in react-navigation-tabs
. Now, we can here see in react-navigation-tabs
that if no tabBarComponent has been passed, it simply uses BottomTabBar, also defined in react-navigation-tabs
. This BottomTabBar
, in turn, takes a custom tab button renderer through props,叫getButtonComponent
。