Aditya Loshali
Sep 15 · 1 min read

Hey Xan, i feel we are trying to avoid the same thing when we say that we don’t want to define a lot of cases in useReducer and make it simpler. Now, you might feel that using lodash and immer is making something that’s over-engineered. but there’s reason to it. That reusable utility is not meant to solve just the number of different cases that we would have to mention in useReducer. but it is also helps you in two other use cases.

1 — Having to update deeply nested state, like you can simply do

updateState({
_path: 'clientDetails.educationalQualfications[0].year',
_value: 1986
})

2 — Having to work based on the previous state.

updateState((prev) => ({ isSideBarOpen: !prev.isSideBarOpen }))

Also, enhancedReducer is a utility that you can define once and use it in as many components you want. The requirement and benefits of it, depend on your use case though. I was managing multi-section form with different kind of inputs. So, this was a gem for me, clean code and less lines of code and less debugging.

    Aditya Loshali

    Written by

    I’m a full stack web developer working with Javascript on React and Node.js everyday.