redux-persist icon indicating copy to clipboard operation
redux-persist copied to clipboard

immer error when using extraReducers option in createSlice

Open erfanasbari opened this issue 3 years ago • 0 comments

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

erfanasbari avatar Jan 26 '22 10:01 erfanasbari