When should we use UseReducer?
We recommend that before watching this video you see the other two posts where we will learn how to handle the React state: React State and Updating React State.
It is very important that we are clear about all the concepts explained in these posts to understand why sometimes we must forego using the useState hook and use the useReducer hook to manage the React state.
In the image above you can read the reasons why we are led to use useReducer instead of useState. We must be clear about all these concepts and understand how we must handle the state in React because otherwise, we can end up writing code that apparently works correctly but can sometimes lead to errors that are difficult to solve. It is essential to know that React updates the state of the components asynchronously and we must handle these situations correctly.
In the video, we can see that at first, we are handling the state using the useState hook and the useEffect hook. In this way, we are perfectly handling the state since useEffect handles the component states as dependencies. This guarantees that we are always handling the last values stored in the states, since this useEffect method will only be executed when the value of the dependencies it handles changes, in this case, the different states.
Leave a Reply
Want to join the discussion?Feel free to contribute!