From the course: Building Production-Ready React Apps: Setup to Deployment with Firebase

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Handle complex logic with useReducer

Handle complex logic with useReducer

- [Instructor] When your app starts to grow, state management needs to be handled very effectively and separately. For that you can use another hook, useReducer, which is a good alternative to useState. So useReducer accepts two entries, first, the function reducer, and another one optional, which is the initialState. Then it returns an array with the current state and the dispatch method that we use to send actions to a function reducer, which is then in charge of returning a new state. And with the new state, we can then update the UI of the components. So let's go back to the source code and see how we can move the business logic and the state management in a separate location. For that, we're going to use useReducer that we're going to add to the scope of this component. Then we're going to write an instance of useReducer that takes two entries. So first the function reducer and the initial state. So we're going to…

Contents