@ -26,9 +26,11 @@ render() {
}
}
```
```
User input has no effect on the rendered element because React has declared the value to be "Hello!". To update the value in response to user input, you would use the onChange event:
User input has no effect on the rendered element because React has declared the value to be "Hello!".
```javascript
To update the value in response to user input, you would use the `onChange` event to save the new value, then pass that to the `value` prop of the form:
```javascript{10,22,23}
class Form extends React.Component {
class Form extends React.Component {
constructor(props) {
constructor(props) {
super(props);
super(props);
@ -91,9 +93,9 @@ An **uncontrolled** component manages its own state.
}
}
```
```
If you wanted to listen to updates to the value, you could use the `onChange` event just like you can with controlled components.
If you wanted to listen to updates to the value, you could use the `onChange` event just like you can with controlled components. However, you would _not_ pass the value you saved to the component.
```javascript
```javascript{10,22}
class Form extends React.Component {
class Form extends React.Component {
constructor(props) {
constructor(props) {
super(props);
super(props);
@ -115,7 +117,6 @@ class Form extends React.Component {
< div >
< div >
< input type = "text"
< input type = "text"
placeholder="Hello!"
placeholder="Hello!"
value={this.state.value}
onChange={this.handleChange} />
onChange={this.handleChange} />
< button onClick = {this.handleSubmit} > Submit< / button >
< button onClick = {this.handleSubmit} > Submit< / button >
< / div >
< / div >
@ -226,7 +227,7 @@ For instance, if you want to imperatively submit a form, one approach would be t
## Examples
## Examples
### Basic Text Input
### Basic Controlled Input
```javascript
```javascript
class Form extends React.Component {
class Form extends React.Component {
@ -236,7 +237,6 @@ class Form extends React.Component {
this.handleChange = this.handleChange.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
}
handleChange(event) {
handleChange(event) {
this.setState({value: event.target.value});
this.setState({value: event.target.value});
}
}
@ -262,7 +262,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
[Try this on CodePen. ](https://codepen.io/ericnakagawa/pen/JRmZjz?editors=0010 )
[Try this on CodePen. ](https://codepen.io/ericnakagawa/pen/JRmZjz?editors=0010 )
### Basic Textarea
### Basic Controlled Textarea
```javascript
```javascript
class Form extends React.Component {
class Form extends React.Component {
@ -300,7 +300,7 @@ ReactDOM.render(<Form />, document.getElementById('root'));
[Try this on CodePen. ](https://codepen.io/ericnakagawa/pen/kkAQPp?editors=0010 )
[Try this on CodePen. ](https://codepen.io/ericnakagawa/pen/kkAQPp?editors=0010 )
### Basic Select
### Basic Uncontrolled Select
```javascript
```javascript
class Form extends React.Component {
class Form extends React.Component {
@ -360,9 +360,12 @@ class Form extends React.Component {
render() {
render() {
return (
return (
< div >
< div >
< input type = "radio" name = "choice" value = "A" onChange = {this.handleChange} / > Option A< br / >
< input type = "radio" name = "choice" value = "A"
< input type = "radio" name = "choice" value = "B" onChange = {this.handleChange} defaultChecked = {true} / > Option B< br / >
onChange={this.handleChange} /> Option A< br / >
< input type = "radio" name = "choice" value = "C" onChange = {this.handleChange} / > Option C< br / >
< input type = "radio" name = "choice" value = "B"
onChange={this.handleChange} defaultChecked={true} /> Option B< br / >
< input type = "radio" name = "choice" value = "C"
onChange={this.handleChange} /> Option C< br / >
< br / >
< br / >
< button onClick = {this.handleSubmit} > Submit< / button >
< button onClick = {this.handleSubmit} > Submit< / button >
< / div >
< / div >
@ -376,38 +379,41 @@ ReactDOM.render(<Form />, document.getElementById('root'));
[Try this on CodePen. ](https://codepen.io/ericnakagawa/pen/WGaYVg?editors=0010 )
[Try this on CodePen. ](https://codepen.io/ericnakagawa/pen/WGaYVg?editors=0010 )
### Basic Checkbox
### Basic Uncontrolled Checkbox
```javascript
```javascript
class Form extends React.Component {
class Form extends React.Component {
constructor(props) {
constructor(props) {
super(props);
super(props);
this.state = {checked: ["B"] };
this.state = {checked: {"A": false, "B": true, "C": false} };
this.handleChange = this.handleChange.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
}
handleChange(event) {
handleChange(event) {
let val = event.target.value;
let value = event.target.value;
let checked = this.state.checked.slice(); // copy
let checked = this.state.checked; // copy
if(checked.includes(val)) {
if (!checked[value]) { checked[value] = true; } else { checked[value] = false; }
checked.splice(checked.indexOf(val), 1);
} else {
checked.push(val);
}
this.setState({checked: checked})
this.setState({checked: checked})
}
}
handleSubmit(event) {
handleSubmit(event) {
alert("Boxes checked are: '" + this.state.checked + "'");
alert("Boxes checked: " +
(this.state.checked.A ? "A " : "") +
(this.state.checked.B ? "B " : "") +
(this.state.checked.C ? "C" : "")
);
}
}
render() {
render() {
return (
return (
< div >
< div >
< input type = "checkbox" value = "A" onChange = {this.handleChange} / > Option A< br / >
< input type = "checkbox" value = "A"
< input type = "checkbox" value = "B" onChange = {this.handleChange} defaultChecked = {true} / > Option B< br / >
onChange={this.handleChange} /> Option A< br / >
< input type = "checkbox" value = "C" onChange = {this.handleChange} / > Option C< br / >
< input type = "checkbox" value = "B"
onChange={this.handleChange} defaultChecked={true} /> Option B< br / >
< input type = "checkbox" value = "C"
onChange={this.handleChange} /> Option C< br / >
< br / >
< br / >
< button onClick = {this.handleSubmit} > Submit< / button >
< button onClick = {this.handleSubmit} > Submit< / button >
< / div >
< / div >