正常情况下父组件想孙组件传值:
父组件把值传给子组件,子组件把值传给孙组件
其实还是有另一种方法
在父组件中设置方法getChildContext
一.在父组件中定义方法
getChildContext(){
return {
color:this.state.color
}
}
二.在父组件中设置数据类型
导包
import ReactTypes from 'prop-types'
设置数据类型
static childContextTypes={
xxx:ReactTypes.
}
三.在孙组件中校验父组件传来的类型
导包
import ReactTypes from 'prop-types'
static contextTypes={
xxx:ReactTypes.
}
而在组组件中获取值的时候通常都使用
this.context.xxx