/
index.jsx
84 lines (81 loc) · 1.46 KB
/
index.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
84
import React, { Component } from 'react'
import './style.scss'
const nContext = React.createContext(0)
function F1(props) {
return (
<div className="bordered">
1111
<F2 />
</div>
)
}
function F2(props) {
return (
<div className="bordered">
2222
<F3 />
</div>
)
}
function F3(props) {
return (
<div className="bordered">
3333
<nContext.Consumer>{x => <F4 n4={x.n} setN={x.setN} />}</nContext.Consumer>
</div>
)
}
function F4(props) {
return (
<div className="bordered">
4444, {props.n4}
<button onClick={props.setN}>Click me</button>
</div>
)
}
// -----------------
// 标签里面传递函数
function Consumer(props) {
// console.log(props.children)
const child = props.children(9202)
return (
<h2>{child}</h2>
)
}
// -----------------
class RA7 extends Component {
constructor() {
super()
this.state = {
x: {
n: 99,
setN: () => {
this.setState({
x: {
...this.state.x,
n: this.state.x.n * 2
}
})
}
}
}
}
render() {
return (
<div>
<div>
<Consumer>
{p => <div>{p}</div>}
{/* {p => <div>{p}</div>} */}
</Consumer>
</div>
<div>
<nContext.Provider value={this.state.x}>
<F1 />
</nContext.Provider>
</div>
</div>
)
}
}
export default RA7