如何等待redux改变prop - getDerivedStateFromProps

时间:2018-04-06 02:05:04

标签: javascript react-native redux

我正在等待redux存储被填充,以便我可以重新加载React Native屏幕,但是在更新此redux状态时刷新页面时出现问题。

具体来说,我正在使用wix的react-native-navigation库并启动了一个基于选项卡的应用程序。我从选项卡的第一个屏幕推送登录屏幕,并且在成功登录/注册之前不要将其弹出。由于此设置,所有默认选项卡屏幕在登录流程完成之前加载,我可以获取用户授权令牌。当我收到此授权令牌时,我想刷新其中一个屏幕,因为我现在可以使用此令牌发出请求。

目前我所拥有的是:

AuthActions.js:

string title = advertisement.Title;
 string desc = advertisement.Desc;

AuthReducer.js

export const facebookLogin = (id, token) => { 
//"token" here isn't the token i need -  i know, confusing
    return dispatch => {
        fetch('https://somethingsomething.com/auth/facebook/'+id+'/'+token)
        .then((response) => response.json())
        .then((responseJson) => {
            console.log(responseJson.token); // < - This is the token I need 
            return facebookSuccess(dispatch, responseJson);
        })
        .catch((error) => {
            console.log(error);
            return facebookFail(dispatch);
        });
    };
};


const facebookSuccess = (dispatch, data) => {
    dispatch({
        type: FACEBOOK_LOGIN_SUCCESS,
        payload: data
    });
};

在ComponentIWantToReRender.js中:

export default (state = INITIAL_STATE, action) => {
    switch (action.type) {
        case FACEBOOK_LOGIN_SUCCESS:
        console.log(action.payload.token); //Successfully logged here
            return {
                ...state,
                ...INITIAL_STATE,
                firstName: action.payload.user.firstName,
                lastName: action.payload.user.lastName,
                user: action.payload.user.id,
                profilePicture: action.payload.user.profilePic,
                authorization: action.payload.token
                };
        default:
                ...
     }
}

在ComponentIWantToReRender.js中,如果我在渲染过程中记录this.props.authorization,则会显示未定义。这是因为尚未通过登录过程/ redux设置它。我需要等到它准备好了,但我不确定该怎么做。有没有人有任何建议或见解?

0 个答案:

没有答案