首页 > 解决方案 > createAsyncThunk: 错误: addCase 不能用两个 reducer 调用相同的操作类型

问题描述

当我将操作连接到extraReducers时发生此错误 我的代码是

export const fetchCountries = createAsyncThunk(
  `country`, 
  async (organizationId: string) => {

export const saveCountry = createAsyncThunk(
  `country`,
  async ({ } => {})

const regions = createSlice({
  name,
  initialState,
  reducers: {},
  extraReducers: builder => {
    builder.addCase(fetchCountries.pending, isFetching);
    builder.addCase(fetchCountries.rejected, error);
    builder.addCase(fetchCountries.fulfilled, (state, action) => {});

    builder.addCase(saveCountry.pending, isFetching);
    builder.addCase(saveCountry.rejected, error);
    builder.addCase(saveCountry.fulfilled, (state, {payload}) => {});

如果我运行,我会收到此错误: Error: addCase cannot be called with two reducers for the same action type

标签: javascriptreduxredux-thunk

解决方案


发生这种情况是因为在我的操作中几乎没有具有相同typePrefix的AsyncThunks操作。

所以它必须有不同的名称:

export const fetchCountries = createAsyncThunk(
  `country/get`, //<------ this first argument (typePrefix) must be unique
  async (organizationId: string) => {

export const saveCountry = createAsyncThunk(
  `country/post`,
  async ({ } => {})


推荐阅读