rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.91k stars 863 forks source link

immer error when using extraReducers option in createSlice #1360

Open erfanasbari opened 2 years ago

erfanasbari commented 2 years ago

Hello. First of all thanks for your hard work that you put on this library.

My problem is that if I use extraReducers option in createSlice method from @reduxjs/toolkit and wrap the reducer using persistReducer I get this error message from immer: An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft

This is my slice:

export interface UserState {
    accesses: Access[];
}

const persistConfig: PersistConfig<UserState> = {
    storage,
    key: "user.profile",
    whitelist: ["accesses"],
};

const userSlice = createSlice({
    name: "profile",
    initialState: {
        accesses: [],
    } as UserState,
    extraReducers: (builder) => {
        builder.addMatcher(profileApi.endpoints.view.matchFulfilled, (state, { payload }) => {
            state.accesses = getAccessesFromRoles(payload.roles);
        });
    },
});

export default persistReducer(persistConfig, userSlice.reducer);

If I don't wrap my reducer in persistReducer the error will go away.

I tried returning a new state instead of mutating the state in extraReducers but it didn't work