DEV Community

Discussion on: 3 Reasons to useReducer() over useState()

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
aisone profile image
Aaron Gong

Why?

Collapse
 
kiszuriwalilibori profile image
Piotr Maksymiuk

What you mean?

Collapse
 
neoprint3d profile image
Drew Ronsman

Explain

Collapse
 
joelmarkbyrd profile image
Joel Byrd

The first point says to use useReducer() when the next state depends on the previous one - he uses a counter as an example, where the next state is one more than the previous state. The point @clarity89 is making is that the useState() setter function provides a format that gives you access to the previous value in a way that gives you a predictable state transition, so useReducer() is not necessary in this case. You can read the documentation here, but basically the form is (using the counter example):

setState(prevCounterValue => prevCounterValue + 1)

So basically the first point is invalid, because useReducer() is not necessary to provide a predictable state transition when the next value depends on the previous value.