From the course: React Essential Training
Unlock this course with a free trial
Join today to access over 24,500 courses taught by industry experts.
Managing state with useReducer - React.js Tutorial
From the course: React Essential Training
Managing state with useReducer
- [Instructor] Let's make some adjustments here to our App.jsx so that we can handle our state management in a slightly different way. So here we're going to import useReducer from React, and we will use that function. This is just another hook that's supplied to us by the React library. We're going to use it here on line 60 within the App component. So I'm just going to comment out our status here, our useState hook, and we're going to refactor it using useReducer. So a couple things to know about whenever we call useReducer, it's going to return an array of two items again. This time the two items are going to be that State variable, like you might expect, as well as some sort of a function that's going to dispatch an event. So the useReducer function is taking in that reducer as the first argument. All right, so we're going to take in a status, we're going to return whatever the opposite of that status is. All right, so all of that being said, we can take this dispatch function and…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.