The useReducer hook is an alternative to the useState hook and is preferable when you have complex state logic or when your next state depends on y...
For further actions, you may consider blocking this person and/or reporting abuse
Nice article, helps me to understand useReducer with TypeScript.
lines with:
value: state.count + payload
value: state.count - payload
should be changed to:
count: state.count + payload
count: state.count - payload
and a few edit in + and - buttons.
I believe
useReducer
is meant for cases where the transitions between the states (actions) are required to understand the underlying logic. In any other case, better useuseState
.there's nothing
useReducer
can do thatuseState
can't, and vice versa. the choice is just about clarity.Exactly. And the choice should depend on whether the transitions are more meaningful to explain the task than the state itself.
On your second use case,
useReducer using the Partial type for state updates
, I think good 'oluseState
is just enough.This is an over simplistic example. What if the payload can span multiple types but depend on the action being called?
There is a typo in your first example
value: state.count + payload,
Should be :
count: state.count + payload,
I appreciated this article, so tyvm! I do think you should implement the changes that @ratrateroo has called out though or at least reply with an explanation of why they're not valid.
Thank you
Between useReducer and useState , which is better for nested objects or arrays?
UseReducer is better for that case