Skip to content

Commit

Permalink
Add a link to "State Updates are Merged" in the forms doc (#8851)
Browse files Browse the repository at this point in the history
* Added a link to "State Updates are Merged"

* better inline links

* moved the explanation down

* Minor unrelated tweaks

(cherry picked from commit eb89bc4)
  • Loading branch information
keyz authored and gaearon committed Jan 23, 2017
1 parent 4f32c39 commit 80b8d5a
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions docs/docs/forms.md
Expand Up @@ -184,15 +184,17 @@ Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select

## Handling Multiple Inputs

When you need to handle multiple controlled `input` elements, you can add a `name` attribute to each element and let a handler function choose what to do based on the value of `event.target.name`. For example:
When you need to handle multiple controlled `input` elements, you can add a `name` attribute to each element and let the handler function choose what to do based on the value of `event.target.name`.

```javascript{15,18,27,34}
For example:

```javascript{15,18,28,37}
class Reservation extends React.Component {
constructor(props) {
super(props);
this.state = {
isGoing: false,
numberOfGuests: 0
isGoing: true,
numberOfGuests: 2
};
this.handleInputChange = this.handleInputChange.bind(this);
Expand All @@ -210,28 +212,31 @@ class Reservation extends React.Component {
render() {
return (
<div>
Is going:
<input
name="isGoing"
type="checkbox"
checked={this.state.isGoing}
onChange={this.handleInputChange}
/>
Number of guests:
<input
name="numberOfGuests"
type="number"
value={this.state.numberOfGuests}
onChange={this.handleInputChange}
/>
</div>
<form>
<label>
Is going:
<input
name="isGoing"
type="checkbox"
checked={this.state.isGoing}
onChange={this.handleInputChange} />
</label>
<br />
<label>
Number of guests:
<input
name="numberOfGuests"
type="number"
value={this.state.numberOfGuests}
onChange={this.handleInputChange} />
</label>
</form>
);
}
}
```

[Try it on CodePen.](https://codepen.io/keyanzhang/pen/pRENvx?editors=0010)
[Try it on CodePen.](https://codepen.io/gaearon/pen/wgedvV?editors=0010)

Note how we used the ES6 [computed property name](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) syntax to update the state key corresponding to the given input name:

Expand All @@ -244,11 +249,13 @@ this.setState({
It is equivalent to this ES5 code:

```js{2}
var nextState = {};
nextState[name] = value;
this.setState(nextState);
var partialState = {};
partialState[name] = value;
this.setState(partialState);
```

Also, since `setState()` automatically [merges a partial state into the current state](/react/docs/state-and-lifecycle.html#state-updates-are-merged), we only needed to call it with the changed parts.

## Alternatives to Controlled Components

It can sometimes be tedious to use controlled components, because you need to write an event handler for every way your data can change and pipe all of the input state through a React component. This can become particularly annoying when you are converting a preexisting codebase to React, or integrating a React application with a non-React library. In these situations, you might want to check out [uncontrolled components](/react/docs/uncontrolled-components.html), an alternative technique for implementing input forms.

0 comments on commit 80b8d5a

Please sign in to comment.