簡體   English   中英

state 在減速器更新 state 之前正在更新

[英]state are being updated before reducer is update the state

我正在使用 userReducer 在整個應用程序中管理用戶 state,但是在使用 reducer 更新 state 時,state 是在 reducer 能夠更新它之前更新的。

檢查

在這里您可以看到之前的 state 變量已更新為有效負載中的新值。

商店.js

import { compose, applyMiddleware } from "redux";
import { legacy_createStore as createStore } from "redux";


import { logger } from "redux-logger";
import { rootReducer } from "./rootReducer";


export const store = createStore(rootReducer,applyMiddleware(logger));

rootReducer.js

import { combineReducers } from "redux";
import { postReducer } from "./posts/posts.reducer";
import { userReducer } from "./user/user.reducer";

export const rootReducer = combineReducers({
  post: postReducer,
  user:userReducer
});

userReducer

import { User_Action_Types } from "./user.types";

const INITIAL_STATE = {
  data: {
  _id: "",
  name: "",
  email: "",
  password: "",
},
};

export const userReducer = (state = INITIAL_STATE, action) => {
  const { type, payload } = action;
  console.log({ action, state });
  switch (type) {
    case User_Action_Types.SetUser: {
      state.data = payload;
      return state;
    }
    case User_Action_Types.ResetUser: {
      return INITIAL_STATE;
    }

    default:
      return state;
  }
};

我嘗試更改操作然后重新安裝模塊但沒有任何效果。 請幫助解決問題。

當前你的變異 state 在減速器中返回新的 state

case User_Action_Types.SetUser: {
      
      return {
             ...state,
             data: payload
      };
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM