From the course: React Hooks

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Handling complex state with useReducer

Handling complex state with useReducer - React.js Tutorial

From the course: React Hooks

Start my 1-month free trial

Handling complex state with useReducer

- [Instructor] In addition to useState, there's another way to manage state variables, and that is another one of these hooks called useReducer. So first, let's import useReducer, and we're going to do a little bit of cleanup here as usual. Instead of returning all that data, we're going to return just an h1, and the h1 should say a number. So the number that we want to display is going to relate to useReducer here. So let's say const number, and then setNumber. This is the function that's going to change the state. This is the state variable. But useReducer is going to look slightly different. Instead of taking in just an initial state, it's going to take in two arguments. So the first argument is actually a function, and the second argument is the initial state. So the initial state for this number is going to be zero. The function that we're going to send as the first argument to useReducer is going to be what we want…

Contents