我正在尝试createAsyncThunk,但无法让extraReducers启动。下面是我现在拥有的:
export const getAllAgents = createAsyncThunk(
"getAllAgents",
async (token: string) => {
let headers = new Headers();
headers.append("Authorization", `Bearer ${token}`);
headers.append("Content-Type", "application/json");
const response = await fetch(`${API.endpoint}/data/agent`, {
method: "get",
headers,
redirect: "follow",
});
const data = await response.json();
console.log("Response in thunk: ", { data });
return data;
}
);
export const agentSlice = createSlice({
name: "agentSlice",
initialState,
reducers: {},
extraReducers:
//builder call back required for typesafety
//https://redux-toolkit.js.org/usage/usage-with-typescript#type-safety-with-extrareducers
(builder) => {
console.log(createNewAgent.fulfilled);
builder.addCase(createNewAgent.fulfilled, (state, action) => {
console.log("fulfilled action", { action });
});
console.log(getAllAgents.fulfilled);
builder.addCase(getAllAgents.fulfilled, (state, action) => {
state.agents = action.payload.agents;
});
},
});
字符串
我还看到在开发工具中调用了已实现的操作:
有什么不对吗??
2条答案
按热度按时间pxyaymoc1#
我也遇到了同样的问题。我的错误是我在extraReducers之外添加了reducers。
这是错误的:
字符串
ffx8fchx2#
我也遇到了类似的问题。原来我忘了加减速器。试试这个。
字符串