Context跨越组件传值
import React from 'react';
const { Provider,Consumer } = React.createContext()
// 祖父 组件 向 孙组件 传值
class Pars extends React.Component {
state = {
num:0
}
render() {
return (
<Provider value={this.state.num}>
<div>
父组件:
<Ch1 />
</div>
</Provider>
)
}
}
class Ch1 extends React.Component {
render() {
return (
<div>
兄弟一:
<Ch2 />
</div>
)
}
}
class Ch2 extends React.Component {
render() {
return (
<div>
兄弟二:
<Consumer>
{
data => <span>{data}</span>
}
</Consumer>
</div>
)
}
}
export {
Pars
}