Skip to content

Commit cc16819

Browse files
committed
上下问实例子
1 parent 1c9bd92 commit cc16819

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/TodoList10/context.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,25 @@ class Main extends Component{
2222
}
2323

2424
class Title extends Component{
25+
static contextTypes ={
26+
color: PropTypes.string
27+
}
2528
render() {
2629
return (
27-
<div>
30+
<div style={{color: this.context.color}}>
2831
这里是标题部分
2932
</div>
3033
)
3134
}
3235
}
3336

3437
class Content extends Component{
38+
static contextTypes ={
39+
color: PropTypes.string
40+
}
3541
render() {
3642
return (
37-
<div>
43+
<div style={{color: this.context.color}}>
3844
这里是内容部分
3945
</div>
4046
)
@@ -43,14 +49,19 @@ class Content extends Component{
4349

4450
class HomePage extends Component {
4551
static childContextTypes = {
46-
color: PropTypes.string
52+
color: PropTypes.string
4753
}
4854
constructor(props) {
4955
super(props);
5056
this.state ={
5157
color: 'red'
5258
}
5359
}
60+
getChildContext() {
61+
return {
62+
color: this.state.color
63+
}
64+
}
5465
render() {
5566
return (
5667
<div>

0 commit comments

Comments
 (0)