-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path7.js
56 lines (46 loc) · 1.35 KB
/
7.js
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
/**
#7 打开和关闭电脑
完成两个组件,电脑 Computer 和显示器 Screen。
电脑有个 status 状态表示电脑现在是开还是关的,status 为 on 为开,status 为 off 为关,默认状态为 off。电脑有个按钮,点击可以自由切换电脑的开关状态。
显示器接受一个名为 showContent 的 props,显示器会把它内容显示出来。如果不传入 showContent,显示器显示 “无内容”。
电脑包含显示器,当电脑状态为开的时候显示器显示“显示器亮了”,否则显示“显示器关了”。
*/
function* genStatus(){
while(1){
yield 'off';
yield 'on';
}
}
class Computer extends Component {
state = {
status: 'off',
}
gen = genStatus();
handleButton = () => {
const status = this.state.status;
this.setState({
status: gen.next().value,
})
}
render () {
return (
<div>
<Screen showContent={this.state.status === 'off' ? '显示器关了' : '显示器亮了'} />
<button onClick={this.handleButton}>开关</button>
</div>
)
}
}
class Screen extends Component {
static defaultProps = {
showContent: '无内容',
}
render () {
const { showContent } = this.props;
return (
<div className='screen'>
{ showContent ? showContent : '无内容'}
</div>
)
}
}