redux-toolkit extraReducers未被调用

afdcj2ne  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(118)

我正在尝试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;
      });
    },
});

字符串
我还看到在开发工具中调用了已实现的操作:

有什么不对吗??

pxyaymoc

pxyaymoc1#

我也遇到了同样的问题。我的错误是我在extraReducers之外添加了reducers。
这是错误的:

const authSlice = createSlice({
  name: 'auth',
  initialState,
  extraReducers: {
    [login.fulfilled]: (state, action) => {},
   },
   [signUp.fulfilled]: state => {}
}

字符串

ffx8fchx

ffx8fchx2#

我也遇到了类似的问题。原来我忘了加减速器。试试这个。

export const store = configureStore({
  reducer: {
    theme: themeSlice,
    user: userSlice // here
  },
});

字符串

相关问题