我们如何使用redux在react-navigation中创建受保护的路由?
考虑到我有一个包含以下状态的redux存储
const mapStateToProps = state => {
return {
isAuthenticated: state.profileInfo.isAuthenticated,
isLoaded: state.profileInfo.isLoaded,
googleProfileLoading: state.profileInfo.googleProfileLoading
}
};
我正在使用React导航来导航用户。
const loginNavigation = createStackNavigator(
{
login: {
screen: Login
},
signup: {
screen: Signup
}
},
{
headerMode: "none"
}
)
const allRoutes = createSwitchNavigator(
{
home: {
screen: loginNavigation
},
user: {
screen: user
},
{
initialRouteName: "home"
}
);
const App = createAppContainer(allRoutes);
现在,如果用户未登录,我想重定向到登录屏幕。
在简单的react-redux中,这通常是我要做的:https://github.com/irohitb/SelfieApp/blob/master/client/src/routes.js
有人可以帮助我弄清楚如何在react-native,redux和react-navigation中创建受保护的路由
答案 0 :(得分:5)
{@ {3}}之类的情况下,反应导航具有createSwitchNavigator
。
您必须将受保护的路由分组到一个MainStack中。使用createStackNavigator
:
const MainStack = createStackNavigator(
{
home: { screen: HomeScreen },
events: { screen: EventsScreen },
profile: { screen: ProfileScreen },
...
{
initialRouteName: "home"
}
);
然后,再次使用createStackNavigator
配置身份验证堆栈:
const AuthStack = createStackNavigator({
login: { screen: LoginScreen },
register: { screen: RegisterScreen },
forgot: { screen: ForgottenPasswordScreen },
...
});
现在createSwitchNavigator
即将到来-加载MainStack
堆栈或AuthStack
:
const Routes = createSwitchNavigator({
initialLoading: InitialLoadingScreen,
auth: AuthStack,
all: MainStack,
}, {
initialRouteName: 'initialLoading',
});
export default createAppContainer(Routes);
无论用户是否通过身份验证,createSwitchNavigator
都会加载保存逻辑的InitialLoadingScreen
:
class InitialLoadingScreen extends React.Component {
constructor(props) {
super(props);
this.bootstrapAsync();
}
bootstrapAsync = async () => {
// Load the home screen for the logged in users
if (this.props.isAuthenticated) {
return this.props.navigation.navigate('home');
}
// load the Auth screen if the user is NOT logged in
this.props.navigation.navigate('login');
}
// Render any loading content that you like here
render() {
return (
<View style={styles.container}>
<ActivityIndicator />
</View>
);
}
}
const mapStateToProps = ({ settings }) => ({
isAuthenticated: state.profileInfo.isAuthenticated,
isLoaded: state.profileInfo.isLoaded,
googleProfileLoading: state.profileInfo.googleProfileLoading
});
export default connect(mapStateToProps)(InitialLoadingScreen);
如您所见,您可以将InitialLoadingScreen
连接到redux存储,以访问任何数据并将其用于路由逻辑:)
答案 1 :(得分:2)
因此,您有一个redux状态变量isAuthenticated
。
在每个屏幕中,您都需要检查该状态,如果未登录,则允许用户登录屏幕并重置导航堆栈。
您可以签入将在首次运行时调用的任何生命周期方法,
例如constructor
,componentWillMount
,componentDidMount
等
用于Redux状态检查,
if(!this.props.isAuthenticated){
this.props.logoutAction();
this.props.navigation.navigate("Login");//Login or whatever your first screen is...
}
为清除整个堆栈,您需要创建我们在上一节中调用的操作。
export const logoutAction = () => {
return dispatch => dispatch({ type: LOGOUT_SUCCESS });
};
您必须在文件中更改一些逻辑,在该文件中编写了组合减速器的代码,
const appReducer = combineReducers({
loginReducers: LoginReducers,
...
...
});
export default (rootReducer = (state, action) => {
if (action.type == LOGOUT_SUCCESS) {
state = undefined;
}
return appReducer(state, action);
});
这将重置整个减速器。
现在,如果要创建注销按钮,则只需将按钮的onPress
更改为isAuthenticated
,然后启动导航方法和注销操作即可。
答案 2 :(得分:0)