You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/forms.md
+26-34
Original file line number
Diff line number
Diff line change
@@ -135,44 +135,36 @@ In HTML, `<select>` creates a drop-down list. For example, this HTML creates a d
135
135
Note that the Coconut option is initially selected, because of the `selected` attribute. React, instead of using this `selected` attribute, uses a `value` attribute on the root `select` tag. This is more convenient in a controlled component because you only need to update it in one place. For example:
136
136
137
137
```javascript{4,10-12,24}
138
-
class FlavorForm extends React.Component {
139
-
constructor(props) {
140
-
super(props);
141
-
this.state = {value: 'coconut'};
142
-
143
-
this.handleChange = this.handleChange.bind(this);
144
-
this.handleSubmit = this.handleSubmit.bind(this);
145
-
}
146
-
147
-
handleChange(event) {
148
-
this.setState({value: event.target.value});
149
-
}
150
-
151
-
handleSubmit(event) {
152
-
alert('Your favorite flavor is: ' + this.state.value);
[**Try it on CodePen**](https://codepen.io/gaearon/pen/JbbEzX?editors=0010)
167
+
[**Try it on CodeSandBox**](https://codesandbox.io/s/controlled-select-example-fls8j)
176
168
177
169
Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select>` all work very similarly - they all accept a `value` attribute that you can use to implement a controlled component.
0 commit comments