DEV Community

Discussion on: Immer vs Ramda - two approaches towards writing Redux reducers

Collapse
 
markerikson profile image
Mark Erikson

Note that you should be using our official Redux Toolkit package, which already comes with Immer built in:

redux.js.org/tutorials/fundamental...

In addition, createSlice also generates your action creators for free and handles all the TS typing based on the payload types that you declare.

Collapse
 
webduvet profile image
webduvet

IMHO redux-toolkit is almost equally ugly as immer. It does not bring anything to the table except extra layering.

Collapse
 
fkrasnowski profile image
Franciszek Krasnowski

I am aware of Redux Toolkit. (I probably should mention that library in the article). However, this article is about the comparison of approaches to writing reducers, and the problems I've mentioned are not limited to Redux so it's not targeting mainly Redux, but Immer and Ramda as well. I've considered Redux as a good foundation for further analysis of those libraries and something that many devs can relate to. I like the way Redux Toolkit implements Redux, Immer, Redux Thunk and automatic action creators. I'm also open to other implementations for example: redux-observable + Ramda.

Collapse
 
markerikson profile image
Mark Erikson

As it turns out, you can use Redux Toolkit even if you're not using a Redux store!

A reducer is, ultimately, just a function. It doesn't matter if that function is implemented using vanilla JS, Immer, Ramda, Lodash/FP, or something else.

I've used Redux Toolkit's createSlice API multiple times to create reducers that were only meant for use with the useReducer hook in a React component, in apps that weren't using a Redux store at all.

If you're looking at using Immer to write a reducer, you should seriously consider just using createSlice instead of calling produce yourself. createSlice adds minimal extra runtime overhead beyond Immer by itself, and works great with TypeScript as well.

Thread Thread
 
fkrasnowski profile image
Franciszek Krasnowski

Yeah, it's a great tool indeed. I've included your thoughts in the article