父组件
class List extends Componet(){
constructor(props){
super(props)
}
static childContextTypes = { // 必须写
color: PropTypes.string
}
getChildContext(){
return {
color: 'res'
}
}
}
子组件
class ListItem extends Componet(){
constructor(props){
super(props)
}
stati contextTypes = { // 必须写
color: PropTypes.string
}
render(){
console.log(this.context.color) // 就可用父组件的数据了
}
}
React context 用法
最新推荐文章于 2025-06-15 11:27:48 发布