简体   繁体   English

加载 state 永远不会成真??(React - Redux)

[英]loading state never get true??(React - Redux)

I am creating an app that get the user data from the backend when the user login and display it in the user profile, i need to set loading statement like loading... in place of the profile before all data comes from the backend then display the data instead我正在创建一个应用程序,当用户登录时从后端获取用户数据并将其显示在用户配置文件中,我需要在所有数据来自后端然后显示之前设置加载语句(如loading... )代替配置文件数据代替

my action creators:我的动作创造者:

export const loginUser = (loginData) => (dispatch) => {

    axios.post('/signin', loginData)
        .then(res => {
            let FBIdToken = `Bearer ${token}`;
            localStorage.setItem('FBIdToken', FBIdToken);
            dispatch(getUserData());
        })
        .catch(err => {
            dispatch({
                type: SET_ERRORS,
                payload: err.response && err.response.data
            });
        });
}

export const getUserData = () => dispatch => {
    dispatch({ type: LOADING_USER });

    axios.get('/user')
        .then(res => {
            dispatch({
                type: SET_USER,
                payload: res.data
            })
        })
        .catch(err => console.log(err))
}

reducer:减速器:

const intialState = {
    authenticated: false,
    loading: false,
    credentials: {},
};

export default (state = intialState, action) => {
    switch (action.type) {
        case SET_USER:
            return {
                authenticated: true,
                loading: false,
                ...action.payload,
            }
            case LOADING_USER:
                return {
                    loading: true,
                    ...state
                }

        default:
            return state;
    }
};

login page:登录页面:

    const handleSubmit = (e) => {  
        e.preventDefault();
        loginUser(loginData);
    }

now when i click the submit button on login page handlesubmit function run the loginUser action creator that in role run the getUserData action creator.现在,当我单击登录页面handlesubmit function 上的提交按钮时,运行loginUser操作创建器,该操作创建器实际上运行getUserData操作创建器。

now in getUserData action creator this action get dispatched {type: LOADING_USER} and this state should be returned from the reducer before the userData come from the backend:现在在 getUserData 操作创建者中,这个操作被调度{type: LOADING_USER}并且这个 state 应该在 userData 来自后端之前从 reducer 返回:

{
  loading: true,
  credentials: {},
  authenticated: false
}

and then the data comes after axios request and change loading to false and set the user credentials, but this not is not the scenario in my case, as loading never get true in my code and loading... never get displayed before the user credentials set as a user profile.. what is the wrong in my code?然后数据在 axios 请求之后出现,并将loading更改为false并设置用户凭据,但这不是我的情况,因为在我的代码中加载永远不会实现并且loading...永远不会在用户凭据之前显示设置为用户配置文件..我的代码有什么问题?

As I mentioned in the comment above, you need to switch the order of loading: true and ...state .正如我在上面的评论中提到的,您需要切换loading: true...state

Reason: (as you guessed it) the order matters when you are spreading an object, especially if the object being spread also contains the loading key inside it.原因:(如您所料)当您传播 object 时,顺序很重要,特别是如果传播的 object 中还包含loading密钥。 So if you keep ...state after the loading: true , it overrides the loading: true with whatever value state has for loading .因此,如果您在loading: true之后保留...state ,它会使用stateloading具有的任何值覆盖loading: true And I guess that's not something we would want.我想这不是我们想要的。 Hence, keep the ...state before loading: true to get the expected result.因此,在加载前保留...state loading: true以获得预期结果。

you are spreading the previous state after your current updated state:您在当前更新的 state 之后传播之前的 state:

case LOADING_USER:
  return {
    loading: true,
    ...state
  }

you need to do reverse:你需要做相反的事情:

case LOADING_USER:
   return {
     ...state,
     loading: true,              
   }

by spreading the state after your loading, your previous state got overrides the new state, but by spreading the previous state first, you overriding only the loading part of your state!通过在加载后传播 state,您之前的 state 会覆盖新的 state,但是通过首先传播之前的 state,您只会覆盖状态的加载部分!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM