redux 身份验证流- react native和导航到< AuthStack>&lt;RootStack

wi3ka0sx  于 2023-06-06  发布在  React
关注(0)|答案(1)|浏览(147)

我一直在阅读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...为什么?

下面是auth.js(action)文件中的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(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;
    }
};
o3imoua4

o3imoua41#

我不知道你的redux实现的全部范围,主要是你如何将userId放入组件的props中。
我猜你用的是“mapStateToProps”
错了!

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

正确

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

相关问题