我使用redux-toolkit库安装了redux store。
这是index.ts
const createStore = () =>
configureStore({
reducer: rootReducer,
devTools: false,
middleware: getDefaultMiddleware => {
const middleware = getDefaultMiddleware({
immutableCheck: false,
serializableCheck: false,
});
if (process.env.NODE_ENV !== "production") {
middleware.push(logger);
}
return middleware;
},
});
export const store = createStore();
export const persistor = persistStore(store);
字符串
这是rootReducer.ts
import { combineReducers, Reducer, AnyAction } from "@reduxjs/toolkit";
import storage from "redux-persist/lib/storage";
import { persistReducer } from "redux-persist";
import userCompnaySlice from "./slices/userCompnaySlice";
const userCompanyDataPersistConfig = {
key: "userCompanyData",
storage,
};
const appReducer = combineReducers({
userCompany: persistReducer(userCompanyDataPersistConfig, userCompnaySlice.reducer),
});
export type RootState = ReturnType<typeof appReducer>;
const rootReducer: Reducer = (state: RootState, action: AnyAction) =>
appReducer(state, action);
export default rootReducer;
型
这是userCompanySlice.ts:
import { createSlice } from "@reduxjs/toolkit";
import { userCompanyReducer } from "../../utils/types/reducerStateTypes";
import { getCompanyInfo } from "../thunks/userCompanyThunk";
const initialState: userCompanyReducer = {
userInfo: {},
companyInfo: {},
};
const userCompanySlice = createSlice({
name: "userCompany",
initialState,
reducers: {
setUserInfo: (state, action) => {
state.userInfo = action.payload;
},
setCompanyInfo: (state, action) => {
state.companyInfo = action.payload;
},
},
extraReducers: {
[getCompanyInfo.pending.type]: (state, action) => {
console.log("pending");
},
[getCompanyInfo.fulfilled.type]: (state, action) => {
console.log("fulfilled");
state.companyInfo = action.payload;
},
[getCompanyInfo.rejected.type]: (state, action) => {
console.log("rejected");
},
},
});
export const { setUserInfo, setCompanyInfo } = userCompanySlice.actions;
export default userCompanySlice;
型
这是userCompanyThunk.ts文件:
import { createAsyncThunk } from "@reduxjs/toolkit";
import axiosApiInstance from "../../services/axios";
import { companyAPI } from "../../shared/api/apiRoutes";
export const getCompanyInfo = createAsyncThunk(
"userCompany/getCompanyInfo",
async () => {
try {
const response = await axiosApiInstance.get(companyAPI);
console.log(response.data["data"]);
console.log("Action type:", getCompanyInfo.fulfilled.type);
return response.data["data"];
} catch (e: any) {
// if (e.response.status >= 400 && e.response.status < 599) {
// throw new Error(e.response.data.message);
// }
console.log(e);
}
}
);
型
现在,当我调用getCompanyInfo
时,它会成功执行&并打印响应。但在userCompanySlice.ts
中的extraReducers
中不会调用此功能。
下面是我执行getCompanyInfo
的方式:
useEffect(() => {
console.log("get company");
dispatch(getCompanyInfo());
getCompany();
}, [dispatch]);
型
该函数在useEffect
中执行,并打印从API获得的响应,但不调用extraReducer
。因此,状态不会更新。
我希望extraReducers
在调用getCompanyInfo
时工作,并设置companyInfo
状态。
我在以前的项目中使用过类似的方法&只是复制粘贴到这个项目中,但由于某些原因,它在这里不起作用。
2条答案
按热度按时间uplii1fm1#
你好更新你额外的reducers语法如下
字符串
nukf8bse2#
你好我想更新你的代码额外的reducer代码。下面是一个例子
字符串