createAsyncThunk:错误:无法使用同一操作类型的两个减速器调用 addCase

Ale*_*nov 21 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}) => {});

Run Code Online (Sandbox Code Playgroud)

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

Ale*_*nov 64

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

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

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

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

Run Code Online (Sandbox Code Playgroud)


小智 6

就我而言,显示了相同的错误消息,但这是一个不同的错误:

.addCase(setAddress.pending, (state, action) => {
    state.setAddressStatus = 'Pending';
})
.addCase(setAddress.fulfilled, (state, action) => {
    state.setAddressStatus = 'Fulfilled';  
})
.addCase(setAddress.fulfilled, (state, action) => { // I repeated fulfilled 
    state.getAddressesStatus = 'Rejected';
    console.error(action.error);
})
            
Run Code Online (Sandbox Code Playgroud)

我花了几分钟才找到问题,可能会对某人有所帮助。