When you initialize a state variable, you can change it using a function called setState. Explore different ways of changing state with methods that are part of a component class.
- [Instructor] We know how to add state variables…to our React applications, but how do we change state?…Well, we're going to use a function called setState,…which is going to change the state variable…within our component.…So let's go ahead and add a method here…that will trigger that change in state.…And it's going to be called toggleOpenClosed.…So toggleOpenClosed is going to…have this function called setState.…SetState is going to take in an object,…which resets the state.…
So it'll have a key called open,…matching our state variable,…and then we'll do whatever the opposite of that is.…So in this case, it's false.…We'll say open.…setState will change it to the opposite, so true.…All right.…The next thing I need to do here is…whenever we're using a constructor method,…we need to bind this.…So, in other words, I need to say this.toggleOpenClosed,…and we'll set this equal to this.toggleOpenClosed.bind this.…
What this'll do is it'll make this accessible…within the context of this custom method.…So now that I've done this,…
- Refactoring elements with JSX
- Creating components
- Adding component properties and methods
- Displaying child components
- Working with props and state
- Conditional rendering
Skill Level Intermediate
1. What Is React?
2. Introduction to React Elements
3. React Components
4. Props and State
5. Additional Library Features
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.