Use immer within redux-actions

Author: mhubigCreated Oct 10, 2018Updated Mar 27, 2020

Hi together,

I found combining redux-actions with immer to be a very powerful combination to avoid redux boilerplate code. See the example. But I think it would be even more elegant if handleActions would natively use immer to produce the new state.

So this

javascript
import produce from 'immer';

const reducer = handleActions(
	{
		[combineActions(
			actionCreators.shoppinglist.fetchDefaultShoppinglist,
			actionCreators.shoppinglist.modifyShoppinglist
		)]: produce((state, { payload }) => {
			state.loading = payload;
		}),
	},
	initialState
);

could be written as:

javascript
const reducer = handleActions(
	{
		[combineActions(
			actionCreators.shoppinglist.fetchDefaultShoppinglist,
			actionCreators.shoppinglist.modifyShoppinglist
		)]: ((state, { payload }) => state.loading = payload,
        },
	initialState
);

Source: redux-utilities/redux-actions