身份验证流程 - 原生反应并导航至和来自 <AuthStack> <RootStack

问题描述 投票:0回答:1

我一直在阅读 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 返回为未定义...为什么?

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

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');
        })
    }
}

这是 auth.js(reducer)文件中我的 REDUCER

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;
    }
};
react-native authentication redux react-navigation
1个回答
0
投票

我不了解 redux 实现的全部范围,主要是如何将 userId 获取到组件的 props 中。

我猜你正在使用“mapStateToProps”。

错了!

    const mapStateToProps = (state) => ({
        userId: state.userId
    })

正确

    const mapStateToProps = (state) => ({
        userId: state.auth.userId
    })

© www.soinside.com 2019 - 2024. All rights reserved.