Languages

React - what is the difference between useState and useReducer?

0 points
Asked by:
Fletcher-Peralta
778

Hi, can someone explain to me what's the difference between useState and useReducer hook?

React documentation says that "useReducer is usually preferable to useState...", so why should I use useReducer instead of useState?

useState example:

useReducer example:

1 answer
0 points
Answered by:
Richard-Bevan
413

The useReducer hook is used to keep a lot of state changes inside it when you have complex state logic. In the useState the whole state has to be overwritten. Reducer is therefore for smarter condition management. Additionally, it helps to organize the logic of the component.

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join