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
Update Lifting State Up not to mix up DOM value with component state (facebook#9032)
* Update Lifting State Up not to mix up DOM value with component state
A few weeks ago when teaching my friend, she got stuck on
`this.state.value` vs. `event.target.value`. As the documentation
talked a lot about "values", and the term value could mean three
different things (values in general, the "value" prop / DOM value of
the <input> component and the value in state/props), it was not weird
that she got a bit confused.
* Rename Lifting State Up onChange props to onTemperatureChange
This is in-line with how the temperature is provided as a prop named `temperature`
* Fix one value prop not being renamed to temperature
* Update codepen examples in Lifting state up documentation
* Update devtools state change to reflect docs change
Copy file name to clipboardExpand all lines: docs/docs/lifting-state-up.md
+54-54Lines changed: 54 additions & 54 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ function BoilingVerdict(props) {
24
24
}
25
25
```
26
26
27
-
Next, we will create a component called `Calculator`. It renders an `<input>` that lets you enter the temperature, and keeps its value in `this.state.value`.
27
+
Next, we will create a component called `Calculator`. It renders an `<input>` that lets you enter the temperature, and keeps its value in `this.state.temperature`.
28
28
29
29
Additionally, it renders the `BoilingVerdict` for the current input value.
30
30
@@ -33,30 +33,30 @@ class Calculator extends React.Component {
33
33
constructor(props) {
34
34
super(props);
35
35
this.handleChange = this.handleChange.bind(this);
36
-
this.state = {value: ''};
36
+
this.state = {temperature: ''};
37
37
}
38
38
39
39
handleChange(e) {
40
-
this.setState({value: e.target.value});
40
+
this.setState({temperature: e.target.value});
41
41
}
42
42
43
43
render() {
44
-
const value = this.state.value;
44
+
const temperature = this.state.temperature;
45
45
return (
46
46
<fieldset>
47
47
<legend>Enter temperature in Celsius:</legend>
48
48
<input
49
-
value={value}
49
+
value={temperature}
50
50
onChange={this.handleChange} />
51
51
<BoilingVerdict
52
-
celsius={parseFloat(value)} />
52
+
celsius={parseFloat(temperature)} />
53
53
</fieldset>
54
54
);
55
55
}
56
56
}
57
57
```
58
58
59
-
[Try it on CodePen.](http://codepen.io/gaearon/pen/Gjxgrj?editors=0010)
59
+
[Try it on CodePen.](http://codepen.io/valscion/pen/VpZJRZ?editors=0010)
60
60
61
61
## Adding a Second Input
62
62
@@ -74,20 +74,20 @@ class TemperatureInput extends React.Component {
74
74
constructor(props) {
75
75
super(props);
76
76
this.handleChange = this.handleChange.bind(this);
77
-
this.state = {value: ''};
77
+
this.state = {temperature: ''};
78
78
}
79
79
80
80
handleChange(e) {
81
-
this.setState({value: e.target.value});
81
+
this.setState({temperature: e.target.value});
82
82
}
83
83
84
84
render() {
85
-
const value = this.state.value;
85
+
const temperature = this.state.temperature;
86
86
const scale = this.props.scale;
87
87
return (
88
88
<fieldset>
89
89
<legend>Enter temperature in {scaleNames[scale]}:</legend>
90
-
<input value={value}
90
+
<input value={temperature}
91
91
onChange={this.handleChange} />
92
92
</fieldset>
93
93
);
@@ -110,7 +110,7 @@ class Calculator extends React.Component {
110
110
}
111
111
```
112
112
113
-
[Try it on CodePen.](http://codepen.io/gaearon/pen/NRrzOL?editors=0010)
113
+
[Try it on CodePen.](http://codepen.io/valscion/pen/GWKbao?editors=0010)
114
114
115
115
We have two inputs now, but when you enter the temperature in one of them, the other doesn't update. This contradicts our requirement: we want to keep them in sync.
116
116
@@ -130,13 +130,13 @@ function toFahrenheit(celsius) {
130
130
}
131
131
```
132
132
133
-
These two functions convert numbers. We will write another function that takes a string `value` and a converter function as arguments and returns a string. We will use it to calculate the value of one input based on the other input.
133
+
These two functions convert numbers. We will write another function that takes a string `temperature` and a converter function as arguments and returns a string. We will use it to calculate the value of one input based on the other input.
134
134
135
-
It returns an empty string on an invalid `value`, and it keeps the output rounded to the third decimal place:
135
+
It returns an empty string on an invalid `temperature`, and it keeps the output rounded to the third decimal place:
136
136
137
137
```js
138
-
functiontryConvert(value, convert) {
139
-
constinput=parseFloat(value);
138
+
functiontryConvert(temperature, convert) {
139
+
constinput=parseFloat(temperature);
140
140
if (Number.isNaN(input)) {
141
141
return'';
142
142
}
@@ -157,15 +157,15 @@ class TemperatureInput extends React.Component {
157
157
constructor(props) {
158
158
super(props);
159
159
this.handleChange = this.handleChange.bind(this);
160
-
this.state = {value: ''};
160
+
this.state = {temperature: ''};
161
161
}
162
162
163
163
handleChange(e) {
164
-
this.setState({value: e.target.value});
164
+
this.setState({temperature: e.target.value});
165
165
}
166
166
167
167
render() {
168
-
const value = this.state.value;
168
+
const temperature = this.state.temperature;
169
169
```
170
170
171
171
However, we want these two inputs to be in sync with each other. When we update the Celsius input, the Fahrenheit input should reflect the converted temperature, and vice versa.
@@ -176,31 +176,31 @@ If the `Calculator` owns the shared state, it becomes the "source of truth" for
176
176
177
177
Let's see how this works step by step.
178
178
179
-
First, we will replace `this.state.value` with `this.props.value` in the `TemperatureInput` component. For now, let's pretend `this.props.value` already exists, although we will need to pass it from the `Calculator` in the future:
179
+
First, we will replace `this.state.temperature` with `this.props.temperature` in the `TemperatureInput` component. For now, let's pretend `this.props.temperature` already exists, although we will need to pass it from the `Calculator` in the future:
180
180
181
181
```js{3}
182
182
render() {
183
-
// Before: const value = this.state.value;
184
-
const value = this.props.value;
183
+
// Before: const temperature = this.state.temperature;
184
+
const temperature = this.props.temperature;
185
185
```
186
186
187
-
We know that [props are read-only](/react/docs/components-and-props.html#props-are-read-only). When the `value` was in the local state, the `TemperatureInput` could just call `this.setState()` to change it. However, now that the `value` is coming from the parent as a prop, the `TemperatureInput` has no control over it.
187
+
We know that [props are read-only](/react/docs/components-and-props.html#props-are-read-only). When the `temperature` was in the local state, the `TemperatureInput` could just call `this.setState()` to change it. However, now that the `temperature` is coming from the parent as a prop, the `TemperatureInput` has no control over it.
188
188
189
-
In React, this is usually solved by making a component "controlled". Just like the DOM `<input>` accepts both a `value` and an `onChange` prop, so can the custom `TemperatureInput` accept both `value` and `onChange` props from its parent `Calculator`.
189
+
In React, this is usually solved by making a component "controlled". Just like the DOM `<input>` accepts both a `value` and an `onChange` prop, so can the custom `TemperatureInput` accept both `temperature` and `onTemperatureChange` props from its parent `Calculator`.
190
190
191
-
Now, when the `TemperatureInput` wants to update its temperature, it calls `this.props.onChange`:
191
+
Now, when the `TemperatureInput` wants to update its temperature, it calls `this.props.onTemperatureChange`:
Note that there is no special meaning to either `value` or `onChange` prop names in custom components. We could have called them anything else, although this is a common convention.
199
+
Note that there is no special meaning to either `temperature` or `onTemperatureChange` prop names in custom components. We could have called them anything else, like name them `value` and `onChange` which is a common convention.
200
200
201
-
The `onChange` prop will be provided together with the `value` prop by the parent `Calculator` component. It will handle the change by modifying its own local state, thus re-rendering both inputs with the new values. We will look at the new `Calculator` implementation very soon.
201
+
The `onTemperatureChange` prop will be provided together with the `temperature` prop by the parent `Calculator` component. It will handle the change by modifying its own local state, thus re-rendering both inputs with the new values. We will look at the new `Calculator` implementation very soon.
202
202
203
-
Before diving into the changes in the `Calculator`, let's recap our changes to the `TemperatureInput` component. We have removed the local state from it, and instead of reading `this.state.value`, we now read `this.props.value`. Instead of calling `this.setState()` when we want to make a change, we now call `this.props.onChange()`, which will be provided by the `Calculator`:
203
+
Before diving into the changes in the `Calculator`, let's recap our changes to the `TemperatureInput` component. We have removed the local state from it, and instead of reading `this.state.temperature`, we now read `this.props.temperature`. Instead of calling `this.setState()` when we want to make a change, we now call `this.props.onTemperatureChange()`, which will be provided by the `Calculator`:
204
204
205
205
```js{8,12}
206
206
class TemperatureInput extends React.Component {
@@ -210,16 +210,16 @@ class TemperatureInput extends React.Component {
210
210
}
211
211
212
212
handleChange(e) {
213
-
this.props.onChange(e.target.value);
213
+
this.props.onTemperatureChange(e.target.value);
214
214
}
215
215
216
216
render() {
217
-
const value = this.props.value;
217
+
const temperature = this.props.temperature;
218
218
const scale = this.props.scale;
219
219
return (
220
220
<fieldset>
221
221
<legend>Enter temperature in {scaleNames[scale]}:</legend>
222
-
<input value={value}
222
+
<input value={temperature}
223
223
onChange={this.handleChange} />
224
224
</fieldset>
225
225
);
@@ -229,13 +229,13 @@ class TemperatureInput extends React.Component {
229
229
230
230
Now let's turn to the `Calculator` component.
231
231
232
-
We will store the current input's `value` and `scale` in its local state. This is the state we "lifted up" from the inputs, and it will serve as the "source of truth" for both of them. It is the minimal representation of all the data we need to know in order to render both inputs.
232
+
We will store the current input's `temperature` and `scale` in its local state. This is the state we "lifted up" from the inputs, and it will serve as the "source of truth" for both of them. It is the minimal representation of all the data we need to know in order to render both inputs.
233
233
234
234
For example, if we enter 37 into the Celsius input, the state of the `Calculator` component will be:
235
235
236
236
```js
237
237
{
238
-
value:'37',
238
+
temperature:'37',
239
239
scale:'c'
240
240
}
241
241
```
@@ -244,12 +244,12 @@ If we later edit the Fahrenheit field to be 212, the state of the `Calculator` w
244
244
245
245
```js
246
246
{
247
-
value:'212',
247
+
temperature:'212',
248
248
scale:'f'
249
249
}
250
250
```
251
251
252
-
We could have stored the value of both inputs but it turns out to be unnecessary. It is enough to store the value of the most recently changed input, and the scale that it represents. We can then infer the value of the other input based on the current `value` and `scale` alone.
252
+
We could have stored the value of both inputs but it turns out to be unnecessary. It is enough to store the value of the most recently changed input, and the scale that it represents. We can then infer the value of the other input based on the current `temperature` and `scale` alone.
253
253
254
254
The inputs stay in sync because their values are computed from the same state:
255
255
@@ -259,33 +259,33 @@ class Calculator extends React.Component {
@@ -294,15 +294,15 @@ class Calculator extends React.Component {
294
294
}
295
295
```
296
296
297
-
[Try it on CodePen.](http://codepen.io/gaearon/pen/ozdyNg?editors=0010)
297
+
[Try it on CodePen.](http://codepen.io/valscion/pen/jBNjja?editors=0010)
298
298
299
-
Now, no matter which input you edit, `this.state.value` and `this.state.scale` in the `Calculator` get updated. One of the inputs gets the value as is, so any user input is preserved, and the other input value is always recalculated based on it.
299
+
Now, no matter which input you edit, `this.state.temperature` and `this.state.scale` in the `Calculator` get updated. One of the inputs gets the value as is, so any user input is preserved, and the other input value is always recalculated based on it.
300
300
301
301
Let's recap what happens when you edit an input:
302
302
303
303
* React calls the function specified as `onChange` on the DOM `<input>`. In our case, this is the `handleChange` method in `TemperatureInput` component.
304
-
* The `handleChange` method in the `TemperatureInput` component calls `this.props.onChange()` with the new desired value. Its props, including `onChange`, were provided by its parent component, the `Calculator`.
305
-
* When it previously rendered, the `Calculator` has specified that `onChange` of the Celsius `TemperatureInput` is the `Calculator`'s `handleCelsiusChange` method, and `onChange` of the Fahrenheit `TemperatureInput` is the `Calculator`'s `handleFahrehnheitChange` method. So either of these two `Calculator` methods gets called depending on which input we edited.
304
+
* The `handleChange` method in the `TemperatureInput` component calls `this.props.onTemperatureChange()` with the new desired value. Its props, including `onTemperatureChange`, were provided by its parent component, the `Calculator`.
305
+
* When it previously rendered, the `Calculator` has specified that `onTemperatureChange` of the Celsius `TemperatureInput` is the `Calculator`'s `handleCelsiusChange` method, and `onTemperatureChange` of the Fahrenheit `TemperatureInput` is the `Calculator`'s `handleFahrehnheitChange` method. So either of these two `Calculator` methods gets called depending on which input we edited.
306
306
* Inside these methods, the `Calculator` component asks React to re-render itself by calling `this.setState()` with the new input value and the current scale of the input we just edited.
307
307
* React calls the `Calculator` component's `render` method to learn what the UI should look like. The values of both inputs are recomputed based on the current temperature and the active scale. The temperature conversion is performed here.
308
308
* React calls the `render` methods of the individual `TemperatureInput` components with their new props specified by the `Calculator`. It learns what their UI should look like.
@@ -316,7 +316,7 @@ There should be a single "source of truth" for any data that changes in a React
316
316
317
317
Lifting state involves writing more "boilerplate" code than two-way binding approaches, but as a benefit, it takes less work to find and isolate bugs. Since any state "lives" in some component and that component alone can change it, the surface area for bugs is greatly reduced. Additionally, you can implement any custom logic to reject or transform user input.
318
318
319
-
If something can be derived from either props or state, it probably shouldn't be in the state. For example, instead of storing both `celsiusValue` and `fahrenheitValue`, we store just the last edited `value` and its `scale`. The value of the other input can always be calculated from them in the `render()` method. This lets us clear or apply rounding to the other field without losing any precision in the user input.
319
+
If something can be derived from either props or state, it probably shouldn't be in the state. For example, instead of storing both `celsiusValue` and `fahrenheitValue`, we store just the last edited `temperature` and its `scale`. The value of the other input can always be calculated from them in the `render()` method. This lets us clear or apply rounding to the other field without losing any precision in the user input.
320
320
321
321
When you see something wrong in the UI, you can use [React Developer Tools](https://github.com/facebook/react-devtools) to inspect the props and move up the tree until you find the component responsible for updating the state. This lets you trace the bugs to their source:
0 commit comments