From the course: React: State Management

Unlock the full course today

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

Handling complex logic with useReducer

Handling complex logic with useReducer - React.js Tutorial

From the course: React: State Management

Handling complex logic with useReducer

- [Instructor] When your app starts to grow, state management needs to be handled very effectively. Now, we want to focus on making the code more flexible and more maintainable, with the help of another hook: useReducer. useReducer is an alternative to useState that provides a more powerful and a more flexible way of managing the state in your components. It is especially useful for handling complex state logic as it allows to separate the state management logic from your components, making the code easier to understand, easier to maintain, and to test. And so, useReducer accept a reducer function and one initialState as parameters. Then, it returns an array with the current states and a dispatch method. So, let's see one example with useReducer. Until now, we have created and kept the state at the top level of the root components app. Instead, we would like to manage the state in a separate location, so let's do that…

Contents