context了解一下
- 上下文环境
- 使用了就要类型校验
在传入context的组件中要校验context类型并返回context1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class App extends Component {
constructor(props){
super(props);
this.state = {
user: 'binyellow'
}
}
static childContextTypes = {
user: propTypes.string
}
getChildContext(){
return this.state
}
render(){
return (
<div>
App
<GrandChild></GrandChild>
</div>
)
}
}
在使用context的组建中校验1
2
3
4
5
6
7
8
9
10
11
12
13class GrandChild extends Component{
static contextTypes = {
user: propTypes.string
}
render(){
console.log(this.context);
return(
<div>
GrandChildName: {this.context.user}
</div>
)
}
}
高阶函数了解一下
- connect函数形如:
connect(mapStateToProps,mapDispatchToProps)(APP) - 实质上是高阶函数中返回了一个高阶组件
怎么实现
1
2
3
4
5
6
7export function connect(mapStateToProps,mapDispatchToProps){
return function(WrappedComponent){
return class ConnectComponent extends Component{
...
}
}
}箭头函数实现
1
2
3
4
5export const connect = (mapStateToProps,mapDispatchToProps)=>(WrappedComponent)=>{
return class ConnectComponent extends Component{
...
}
}
connect是什么:connect其实就是给传入的组件包裹上context的属性,来拿到全局的属性store
正题:connect如何实现
1 | export const connect = (mapStateToProps=state=>state,mapDispatchToProps={})=>(WrappedComponent)=>{ |