【问题标题】:React-native redux - this.props are undefined from AsyncStorageReact-native redux - this.props 未从 AsyncStorage 定义
【发布时间】:2020-03-31 17:41:33
【问题描述】:

作为RNRedux 的新手,我很困惑为什么在阅读AsyncStorage 之后我的道具未定义。

我登录,将状态保存到商店和存储中...我重新加载应用程序并从存储中读取并更新状态。存储正在检索我的对象,但道具未定义。

actions.js:

export const getSession = (data) => ({
    type: 'GET_SESSION',
    payload: {
        user: data
    }
});

export const getUserSession = () => dispatch => {
    return AsyncStorage.getItem('userSession').then((data) => {
        console.log('Props at asynsstorage: ', data);
        // {"current_user":{"uid":"1","roles":["authenticated","administrator"], ...}
        dispatch(loading(false));
        dispatch(getSession(data));
    })
    .catch((err) => {
    })
}

reducer.js

import { combineReducers } from 'redux';

const defaultState = {
    xcsrf: '',
    user: {},
    loading: false,
    error: '',
};

const authReducer = ( state = defaultState, action ) => {
    switch(action.type) {
        case 'GET_SESSION':
            return {
            ...state,
                user: action.payload.user,
                loading: false,
            }

        case 'SAVE_SESSION':
            return {
                ...state,
                user: action.payload.user,
                loading: false,
            }

        default:
            return state;
    }
}

export default combineReducers({
    authReducer: authReducer
});

authLoading.js // 屏幕

class AuthLoadingScreen extends React.Component {
    constructor() {
        super();
    }

    componentDidMount = () => {
        this.props.getUserSession().then(() => {
            console.log( 'Props at loading: ', this.props.user );
            // undefined

        })
        .catch(error => {
        })
    };

    // Render any loading content that you like here
    render() {
        return ();
    }
}

const mapStateToProps = state => ({
    user: state.user,
});


const mapDispatchToProps = dispatch => ({
    getUserSession: () => dispatch(getUserSession()),
});

export default connect(mapStateToProps, mapDispatchToProps)(AuthLoadingScreen);

【问题讨论】:

标签: javascript react-native react-redux redux-thunk


【解决方案1】:

你不能直接访问reducer的user。所以改变

const mapStateToProps = state => ({
    user: state.user,
});

const mapStateToProps = state => ({
    user: state.authReducer.user,
});

还有一件事AsyncStoragegetItem() 方法返回存储数据的字符串。您还没有将其转换为 json。所以也请转换如下:

export const getUserSession = () => dispatch => {
    return AsyncStorage.getItem('userSession').then((data) => {
        console.log('Props at asynsstorage: ', data);
        // {"current_user":{"uid":"1","roles":["authenticated","administrator"], ...}
        dispatch(loading(false));
        dispatch(getSession(JSON.parse(data))); //convert to json here
    })
    .catch((err) => {
    })
}

【讨论】:

  • 感谢您的警告;我已经对对象进行了字符串化和解析——为了简单起见,只是将其删除。 state.authReducer.user 成功了。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多