我从React Native开始,这里有一个小问题。我有一个bottomTabNavigator,如果用户有权限,他会导航到ImageScreen,否则,它会导航到HomeScreen。
我的函数global.hasPermission()检查权限并返回true或false,因此我希望能够根据函数返回的内容更改{screen: ImageScreen}。我该怎么做呢?我在哪里调用我的函数hasPermission()?
这是我的tabNavigator:
const BottomTab = createBottomTabNavigator({
Image: {
screen: ImageScreen,
navigationOptions: {
tabBarLabel: 'Image Screen',
tabBarIcon: ({ tintColor, focused }) => (
<Ionicons
name={'ios-camera'}
size={focused ? 30 : 26}
style={{ color: tintColor }}
/>
),
},
},
});发布于 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中):
// into your ImageScreen
componentDidMount () {
const hasPermission = YourFunctionWhichReturnsTrueOrFalse()
if (!hasPermission) this.props.navigation.navigate('home') // the title of your home screen here
}https://stackoverflow.com/questions/56022619
复制相似问题