From the course: React Hooks

Unlock the full course today

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

Building a checkbox with useState

Building a checkbox with useState - React.js Tutorial

From the course: React Hooks

Start my 1-month free trial

Building a checkbox with useState

- [Instructor] Another way that we can take advantage of useState, is to use it in combination with inputs. Here, we're going to make the status of a checkbox, checked or unchecked, a state variable. So the first thing we want to do is let's get rid of our old code here. We'll get rid of that useState call. Then inside of this div, we're going to add an input. And the input is going to have a type and that type is checkbox. And then we also want create a state variable. So this time it's going to be checked and setChecked. We'll call useState, and then the initial state is going to be false. So now I can use the value of checked inside of this input. So I'll say that the value is checked. Let's also add a paragraph tag next to this, and we'll say, "If it's checked, then say checked, "otherwise display not checked." Okay. So at this point, it shows our initial state that's working as we expect it to, but we're still not…

Contents