React native 带有StackNavigation的react native expo中的条件主屏幕
我试图制作一个有条件的主屏幕,如果用户有一个“UserID”保存到手机上,它应该把他们带到“PlacesScreen”,否则让它加载“loginsScreen”。我正在与Redux一起使用StackNavigation,学习react native大约两周了React native 带有StackNavigation的react native expo中的条件主屏幕,react-native,redux,React Native,Redux,我试图制作一个有条件的主屏幕,如果用户有一个“UserID”保存到手机上,它应该把他们带到“PlacesScreen”,否则让它加载“loginsScreen”。我正在与Redux一起使用StackNavigation,学习react native大约两周了 function nav (state = firstState(), action) { let nextState; switch (action.type) { case 'Home': nextStat
function nav (state = firstState(), action) {
let nextState;
switch (action.type) {
case 'Home':
nextState = AppNavigator.router.getStateForAction(
NavigationActions.navigate({routeName: 'Home'}),
state
);
return nextState;
break;
case NavActionTypes.NAVIGATE_PLACES:
nextState = AppNavigator.router.getStateForAction(
NavigationActions.navigate({ routeName: 'Places' }),
state
);
return nextState;
break;
}
…等等
我想将减速机的初始状态设置为取决于天气,或者手机上没有保存ID。我是一个新的反应本机和编码一般,im 19,所以任何帮助将不胜感激!如果您需要更多代码来帮助,请让我知道,我会发送github链接。
提前谢谢你!
Cooper正如redux所建议的那样,保持减速器的纯净,我会将导航逻辑从减速器中删除,并将其放入组件中。 此外,我建议在组件装载期间检查LoginScreen上的状态,如果状态包含用户ID,则将导航状态重置为PlacesScreen e、 g
import store from '../storeLocation'
export class LoginScreen extends React.Component {
...
componentWillMount(){
if(store.getState().userID !== "")
this.props.navigation.dispatch(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Places'})
]
}));
}
...
}