From the course: React Foundations by Pearson

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Accessing elements in a controlled component

Accessing elements in a controlled component - React.js Tutorial

From the course: React Foundations by Pearson

Accessing elements in a controlled component

In this section, we're going to see how to access elements in a controlled component. This is another React-specific approach, an alternative to using uncontrolled components. Generally, this is the preferred approach because it's quite straightforward and quite idiomatic, and you'll see it a lot in React code. So this is how it works. Basically, imagine you had a text box where you wanted to hold the description of something. You would declare a variable in react state to represent the item in the text box and then you would two-way bind that state variable directly to an input field. So imagine in react state you've declared a variable, effectively called description, and you've held that value in react state. That's the current value, if you like, that you want to use. And then you've got a text box down here and you want that text box to basically stay in sync with the value in state. So imagine this is some kind of input text box like so. So imagine you set the value in the state…

Contents