身份验证流程-响应本机,并在<AuthStack> <RootStack之间来回导航

时间:2020-10-27 02:00:13

标签: react-native authentication redux react-navigation-v5 redux-logger

我一直在阅读React Navigation身份验证文档,但似乎无法像示例中所描述的那样进行身份验证-https://reactnavigation.org/docs/auth-flow/

我的身份验证中包含以下代码(使用redux)

Class App extends Component{
render(){
 return (
      (this.props.userId == null ? <AuthStack /> : <RootStack />)
    )
  }
}

我的Redux-Logger向我发送以下消息,但是正如您从我的console.log中看到的那样,this.props.userId返回为undefined ...为什么?

enter image description here

这是我在auth.js(操作)文件中的ACTION

export const getUserId = () => {
    return async dispatch => {
        await AsyncStorage.getItem('userId').then(userId => {
            console.log(userId)
            dispatch(setUserId(userId))
            // this.props.navigation.navigate(userId ? 'Main' : 'Login');
        })
    }
}

这是我的REDUCER在auth.js(还原器)文件中

const initialState = {
    userId: ''
};

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case SET_USERID:
            return {
                ...state,
                userId: action.payload
            };
        case REMOVE_USERID:
            return {
                ...state,
                userId: null
            };
        default:
            return state;
    }
};

0 个答案:

没有答案