createAsyncThunk:错误:无法使用两个 reducer 调用相同操作类型的 addCase

Posted

技术标签:

【中文标题】createAsyncThunk:错误:无法使用两个 reducer 调用相同操作类型的 addCase【英文标题】:createAsyncThunk: Error: addCase cannot be called with two reducers for the same action type 【发布时间】:2021-09-23 06:35:43 【问题描述】:

当我将操作连接到 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

【问题讨论】:

【参考方案1】:

发生这种情况是因为在我的操作中很少有 AsyncThunks 操作具有相同的 typePrefix

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

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

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

【讨论】:

感谢您帮助减少查找 thunk 复制粘贴错误所需的时间 :)

以上是关于createAsyncThunk:错误:无法使用两个 reducer 调用相同操作类型的 addCase的主要内容,如果未能解决你的问题,请参考以下文章