Javascript 为什么我的redux状态没有更新

Javascript 为什么我的redux状态没有更新,javascript,reactjs,redux,immutability,thunk,Javascript,Reactjs,Redux,Immutability,Thunk,状态未更新。当调度操作时,状态应更新为isAuthenticated为true..但状态未更新..重新复制返回初始状态而不是更新状态 export function setCurrentUser(user) { console.log(user); return { type: "SET_CURRENT_USER", user }; } ... export function login(userData

状态未更新。当调度操作时,状态应更新为isAuthenticated为true..但状态未更新..重新复制返回初始状态而不是更新状态

export function setCurrentUser(user) {
      console.log(user);
      return {
        type: "SET_CURRENT_USER",
        user
      };
    }

   ...
    export function login(userData) {
      return dispatch => {
        return axios.post('/user/auth',userData).then((res) => {
          const { status, sessionId, username, error} = res.data;
          if(status === "success"){
            dispatch(setCurrentUser(sessionId));
          }else {
            dispatch(invalidUser(error));
          }
        });
      }
    }

//reducers

    import { SET_CURRENT_USER, INVALID_USER } from "../actions/types";
    import isEmpty from "lodash/isEmpty";

    const initialState = {
      isAuthenticated : false,
      user: {},
      error:{}
    };

    export default (state = initialState, action) => {
      switch(action.type){
        case SET_CURRENT_USER:
        return {
          ...state,
         isAuthenticated: !isEmpty(action.user),
         user:action.user
        }
        ...
        default: return state;
      }
    }
//组成部分

onSubmit(e){
    e.preventDefault();
      this.setState({ errors: {}, isLoading:true });
      this.props.login(this.state).then(
      (res) => {
        console.log(this.props.userData);
        if(this.props.userData.isAuthenticated)
          this.context.router.push("greet");
        },
(err)=>this.setState({errors:err.response.data,isLoading:false}) );

  }


您没有更新减速器中的有效负载数据

export default (state = initialState, action) => {
      switch(action.type){
        case SET_CURRENT_USER:
        return {...state, user: action.payload} //update user here
        }
        ...
        default: return state;
      }
    }
上述代码使用es6功能,假设
isAuthenticated
error
是初始状态的一部分


您只是返回了相同的JSON,这就是您的状态没有更新的原因。

更新它的组件代码在哪里?组件添加返回{14 |…状态,>15 |{user:action.user}^16 | 17 |}语法错误您能详细说明一下吗?我试图执行您的代码..但是得到语法错误在哪里?哪一行?您在使用es6吗,babel?返回{…state,>{user:action.payload}}//是的,我在使用es6 babel
export default (state = initialState, action) => {
      switch(action.type){
        case SET_CURRENT_USER:
        return {...state, user: action.payload} //update user here
        }
        ...
        default: return state;
      }
    }