This error occurred when I connect actions to extraReducers My code is
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}) => {});
and if I run I get this error:
Error: addCase cannot be called with two reducers for the same action type
This happens because in my actions there is few AsyncThunks actions with the same typePrefix.
So it must have different names:
export const fetchCountries = createAsyncThunk(
`getCountry`, //<------ this first argument (name) must be unique
async (organizationId: string) => {
export const saveCountry = createAsyncThunk(
`postCountry`,
async ({ } => {})
In my case, the same error message was show, but it was a different mistake:
.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);
})
It took me some minutes to find the problem, may help someone.
On CreateAsycThunk you have mentioned both the string of the same name it should be like this
export const fetchCountries = createAsyncThunk(
`country`,
async (organizationId: string) => {
export const saveCountry = createAsyncThunk(
`saveCountry`,
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}) => {});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With