Skip to content
This repository was archived by the owner on Oct 3, 2021. It is now read-only.

Commit 6b04446

Browse files
committedAug 13, 2021
Lifting state up
1 parent 8a0750d commit 6b04446

File tree

1 file changed

+77
-65
lines changed

1 file changed

+77
-65
lines changed
 

‎src/index.js

+77-65
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,99 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33

4-
class NameForm extends React.Component {
4+
function BoilingVerdict(props) {
5+
if (props.celsius >= 100) {
6+
return <p>The water would boil.</p>;
7+
}
8+
return <p>The water would not boil.</p>;
9+
}
10+
11+
function toCelsius(fahrenheit) {
12+
return (fahrenheit - 32) * 5 / 9;
13+
}
14+
15+
function toFahrenheit(celsius) {
16+
return (celsius * 9 / 5) +32;
17+
}
18+
19+
function tryConvert(temperature, convert) {
20+
const input = parseFloat(temperature);
21+
if (Number.isNaN(input)) {
22+
return '';
23+
}
24+
const output = convert(input);
25+
const rounded = Math.round(output * 1000) / 1000;
26+
return rounded.toString();
27+
}
28+
29+
const scaleNames = {
30+
c: 'Celsius',
31+
f: 'Fahrenheit'
32+
};
33+
34+
class TemperatureInput extends React.Component {
535
constructor(props) {
636
super(props);
7-
this.state = {
8-
name: '',
9-
essay: 'Please write an essay about your favorite DOM element.',
10-
flavor: 'coconut',
11-
isGoing: true,
12-
numberOfGuests: 2
13-
};
37+
this.handleChange = this.handleChange.bind(this);
38+
}
1439

15-
this.handleInputChange = this.handleInputChange.bind(this);
16-
this.handleSubmit = this.handleSubmit.bind(this);
40+
handleChange(e) {
41+
this.props.onTemperatureChange(e.target.value);
1742
}
1843

19-
handleInputChange(event) {
20-
const target = event.target;
21-
const value = target.type === 'checkbox' ? target.checked : target.value;
22-
const name = target.name;
44+
render() {
45+
const temperature = this.props.temperature;
46+
const scale = this.props.scale;
47+
return (
48+
<fieldset>
49+
<legend>Enter temperature in {scaleNames[scale]}:</legend>
50+
<input value={temperature}
51+
onChange={this.handleChange} />
52+
</fieldset>
53+
);
54+
}
55+
}
2356

24-
this.setState({
25-
[name]: value
26-
});
57+
class Calculator extends React.Component {
58+
constructor(props) {
59+
super(props);
60+
this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
61+
this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
62+
this.state = {temperature: '', scale: 'c'};
2763
}
2864

29-
handleSubmit(event) {
30-
alert('A name was submitted: ' + this.state.name);
31-
alert('An essay was submitted: ' + this.state.essay);
32-
alert('Your favorite flavor is: ' + this.state.flavor);
33-
alert('Is going: ' + this.state.isGoing);
34-
alert('Number of guests: ' + this.state.numberOfGuests);
35-
event.preventDefault();
65+
handleCelsiusChange(temperature) {
66+
this.setState({scale: 'c', temperature});
67+
}
68+
69+
handleFahrenheitChange(temperature) {
70+
this.setState({scale: 'f', temperature});
3671
}
3772

3873
render() {
74+
const scale = this.state.scale;
75+
const temperature = this.state.temperature;
76+
const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;
77+
const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
78+
3979
return (
40-
<form onSubmit={this.handleSubmit}>
41-
<lable>
42-
Name:
43-
<input name="name" type="text" value={this.state.name} onChange={this.handleInputChange} />
44-
</lable><br />
45-
<lable>
46-
Essay:
47-
<textarea name="essay" value={this.state.essay} onChange={this.handleInputChange} />
48-
</lable><br />
49-
<label>
50-
Pick your favorite flavor:
51-
<select name="flavor" value={this.state.flavor} onChange={this.handleInputChange}>
52-
<option value="grapefruit">Grapefruit</option>
53-
<option value="lime">Lime</option>
54-
<option value="coconut">Coconut</option>
55-
<option value="mango">Mango</option>
56-
</select>
57-
</label><br />
58-
<label>
59-
Is going:
60-
<input
61-
name="isGoing"
62-
type="checkbox"
63-
checked={this.state.isGoing}
64-
onChange={this.handleInputChange} />
65-
</label><br />
66-
<label>
67-
Number of guests:
68-
<input
69-
name="numberOfGuests"
70-
type="number"
71-
value={this.state.numberOfGuests}
72-
onChange={this.handleInputChange} />
73-
</label><br />
74-
<lable>
75-
Read only:
76-
<input type="text" value="hi" />
77-
</lable><br />
78-
<input type="submit" value="Submit" />
79-
</form>
80+
<div>
81+
<TemperatureInput
82+
scale="c"
83+
temperature={celsius}
84+
onTemperatureChange={this.handleCelsiusChange} />
85+
<TemperatureInput
86+
scale="f"
87+
temperature={fahrenheit}
88+
onTemperatureChange={this.handleFahrenheitChange} />
89+
<BoilingVerdict
90+
celsius={parseFloat(celsius)} />
91+
</div>
8092
);
8193
}
8294
}
8395

8496
ReactDOM.render(
85-
<NameForm />,
97+
<Calculator />,
8698
document.getElementById('root')
8799
);

0 commit comments

Comments
 (0)