From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

useReducer vs useState: Basic example

useReducer vs useState: Basic example

Hello and welcome back. Before starting on with the useReducerHook example, let's first go through how a useReducerHook is different or it can be compared with useStateHook. We'll keep it a scenario and see if the useStateHook and the useReducerHook is viable to that scenario or not. So when you have simple state like 1 or 2 values, the useState hook is and should be preferred and using the useReducer hook will be an overkill, but it can do this. If you have a complex state object, which requires multiple state transitions, then Using the useState hook, you can achieve it, but it is going to become messy, whereas the useReducer is preferred choice. When your state depends on the previous state values, it's hard to manage using the useState, but it becomes very easy using the useReducer. and when you have multiple actions which are going to modify the state value for example moving the record to pending to in progress or from in progress to in review so you can see that there are…

Contents