React
context的用法:
- react是单向数据流,我们想传递数据需要一层层向下传递,数据传递变得非常麻烦,我们可以用context实现数据的交互
- 父组件向子组件传递数据的方法
<-- 定义子组件上下文的类型 -->
static childContextTypes = {
store:PropTypes.object,
};
<-- 定义子组件上下文的数据 -->
getChildContext(){
return {
store:store,
}
}
- 子孙组件承接父组件数据的方法
<-- 子组件 contextTypes函数,用来放置从父组件一层层传递下来的数据 -->
static contextTypes = {
store: PropTypes.object
};