首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >底部选项卡中的条件

底部选项卡中的条件
EN

Stack Overflow用户
提问于 2019-05-07 20:25:41
回答 1查看 62关注 0票数 0

我从React Native开始,这里有一个小问题。我有一个bottomTabNavigator,如果用户有权限,他会导航到ImageScreen,否则,它会导航到HomeScreen。

我的函数global.hasPermission()检查权限并返回true或false,因此我希望能够根据函数返回的内容更改{screen: ImageScreen}。我该怎么做呢?我在哪里调用我的函数hasPermission()?

这是我的tabNavigator:

代码语言:javascript
复制
const BottomTab = createBottomTabNavigator({
    Image: {
        screen: ImageScreen,
        navigationOptions: {
            tabBarLabel: 'Image Screen',
            tabBarIcon: ({ tintColor, focused }) => (
                <Ionicons
                    name={'ios-camera'}
                    size={focused ? 30 : 26}
                    style={{ color: tintColor }}
                />
            ),
        },
    },
});
EN

回答 1

Stack Overflow用户

发布于 2019-05-07 21:04:41

在我的应用程序中,我通过React Context API ( https://reactjs.org/docs/context.html )处理身份验证

几天前,我回答了一个类似的问题,你可以在这里看到如何使用上下文创建:How Redirect to Login if page is protected and the user is not signed in?

就像我之前的回答一样,你可以在ImageScreen的componentDidMount ()中检查用户权限,如果他没有权限,你可以像这样将它重定向到你的主屏幕(假设你的主屏幕包含在Stack Navigator中):

代码语言:javascript
复制
// into your ImageScreen
componentDidMount () {
  const hasPermission = YourFunctionWhichReturnsTrueOrFalse()
  if (!hasPermission) this.props.navigation.navigate('home') // the title of your home screen here
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56022619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档