createAsyncThunk: 错误: addCase 不能用两个 reducer 调用相同的操作类型

时间:2021-07-14 09:24:02

标签: javascript redux redux-thunk

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

答案 0 :(得分: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 ({ } => {})