A controlled component accepts a value as a prop and also uses a callback to change a value. This way of creating a form removes the need for refs in your app.
- [Narrator] We just looked at uncontrolled components…and how we can use refs to capture values from inputs.…In this video, we'll look at controlled components.…A controlled input accepts a value as a prompt…and also uses a call back to change a value.…So, let's make some adjustments to our form here.…Right now, we have our handleSubmit function,…let's stub out a few more things that we're gonna need.…So the first thing here is state.…So, our state property is going to…have a value called Message,…and the initial state will be just an empty string.…
Next, we want to create something called handleChange.…And handleChange is gonna be another function…that does something in a second.…So, we looked at how to add a ref to an input,…let's actually get rid of this though.…Instead, we're going to use the value this.state.message,…and then when something changes,…we're going to call this.handleChange.…
Okay, so let's create our handleChange function.…This is going to also set the state.…And what this will do, is it's going to reset the state…
- What's new in React 16
- Rendering arrays
- Controlled, uncontrolled, and compound components
- Using keyed fragments
- Working with new lifecycle methods
- React patterns
- Using the context API
- Checking for accessibility issues in your React code
Skill Level Advanced
1. What’s New in React
2. Advanced Components
3. Enhanced Components
4. React Patterns
5. React Accessibility
- 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.