-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-ClassWithLifecycle(2).jsx
83 lines (75 loc) · 2.52 KB
/
11-ClassWithLifecycle(2).jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React from 'react';
import Row from './Row';
import { ThemeContext, LocaleContext } from './context';
export default class Greeting extends React.Component {
constructor(props) {
super(props);
this.state = {
name = "Mary",
surname = "Poppins",
width: window.innerWidth
}
this.handleNameChange = this.handleNameChange.bind(this);
this.handleSurnameChange = this.handleSurnameChange.bind(this);
this.handleResize = this.handleResize.bind(this);
}
componentDidMount() {
// contains two unrelated lines, may make it difficult to isolate for testing
document.title = `${this.state.name} ${this.state.surname}`;
window.addEventListener('resize', this.handleResize)
}
componentDidUpdate() {
document.title = `${this.state.name} ${this.state.surname}`;
}
// we also want to unsubscribe to prevent memory leak
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize)
}
handleResize() {
this.setState({
width: window.innerWidth
});
}
handleNameChange(e) {
this.setState({
name: e.target.value
});
}
handleSurnameChange(e) {
this.setState({
surname: e.target.value
});
}
render() {
return (
<ThemeContext.Consumer>{
theme => (
<section className={theme}>
<Row label="Name">
<input
value={this.state.name}
onChange={this.handleNameChange}
/>
</Row>
<Row label="Surname">
<input
value={this.state.surname}
onChange={this.handleSurnameChange}
/>
</Row>
<LocaleContext.Consumer>
{locale => (
<Row label="Language">
{locale}
</Row>
)}
</LocaleContext.Consumer>
<Row label="width">
{this.state.width}
</Row>
</section>
)}
</ThemeContext.Consumer>
)
}
}