企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ import React from 'react'; let ThemeContext = createContext(); function createContext(){ class Provider extends React.Component{ static value; constructor(props){ super(props);//{value:xx} Provider.value = props.value; this.state = {number:0}; } /* componentWillReceiveProps(nextProps){ Provider.value = nextProps.value; } */ //每当此组件接收到新的属性的时候,都会执行这个方法,这个方法会返回新的状态对象 static getDerivedStateFromProps(nextProps,prevState){ Provider.value = nextProps.value; return prevState; } render(){ return this.props.children; } } class Consumer extends React.Component{ render(){ return this.props.children(Provider.value); } } return {Provider,Consumer} } //ThemeContext {Provider,Consumer} class Header extends React.Component{ static contextType = ThemeContext; constructor(props){//pros属性对象 context 上下文对象 super(props); } render(){ this.context2 = Content.contextType.Provider.value; return ( <div style={{border:`5px solid ${this.context2.color}`,padding:5}}> Header <Title/> </div> ) } } /* class Title extends React.Component{ static contextType = ThemeContext; render(){ return ( <div style={{border:`5px solid ${this.context.color}`,padding:5}}> Title </div> ) } } */ function Title(){ return ( <ThemeContext.Consumer> { value=>( <div style={{border:`5px solid ${value.color}`,padding:5}}> Title </div> ) } </ThemeContext.Consumer> ) } class Main extends React.Component{ static contextType = ThemeContext; constructor(props){//pros属性对象 context 上下文对象 super(props); } render(){ this.context2 = Content.contextType.Provider.value; return ( <div style={{border:`5px solid ${this.context2.color}`,padding:5}}> Main <Content/> </div> ) } } class Content extends React.Component{ static contextType = ThemeContext; constructor(props){//pros属性对象 context 上下文对象 super(props); } render(){ this.xxx = Content.contextType.Provider.value; return ( <div style={{border:`5px solid ${this.context2.color}`,padding:5}}> Content <button onClick={()=>this.context2.changeColor('red')}>变红</button> <button onClick={()=>this.context2.changeColor('green')}>变绿</button> </div> ) } } class Page extends React.Component{ constructor(props){ super(props); this.state = {color:'green'}; } changeColor = (color)=>{ this.setState({color}); }; render(){ let value = {color:this.state.color,changeColor:this.changeColor}; console.log('Page.render',value); return ( <ThemeContext.Provider value={value}> <div style={{border:`5px solid ${this.state.color}`,padding:5}}> Page <Header/> <Main/> </div> </ThemeContext.Provider> ) } } export default Page; ~~~ ![](https://img.kancloud.cn/d5/89/d5896c89f2ec29de9045c7dc0f63cce0_1037x435.png) ~~~ constructor实例化的时候执行一次, this.context2 = Content.contextType.Provider.value; // 放在render里面,每次更新都会渲染 ~~~ ![](https://img.kancloud.cn/db/04/db0430cfd8b3338792e7cd42d7fdb8c7_982x511.png) ![](https://img.kancloud.cn/90/86/9086096d46afba70e5d368eca2aa835f_751x279.png) ![](https://img.kancloud.cn/32/5e/325e709c894cb8fc09f4a02a92c33af3_738x171.png) ~~~ import React from 'react'; import PropTypes from 'prop-types'; class Main extends React.Component{ constructor(props){//pros属性对象 context 上下文对象 super(props); } render(){ return ( <div style={{border:`5px solid ${this.context2.color}`,padding:5}}> Main <Content/> </div> ) } } class Content extends React.Component{ constructor(props,context){//pros属性对象 context 上下文对象 super(props,context); this.state = {color:context.color}; } contextTypes = { color:PropTypes.string, changeColor:PropTypes.func } render(){ return ( <div style={{border:`5px solid ${this.state.color}`,padding:5}}> Content </div> ) } } class Page extends React.Component{ constructor(props){ super(props); this.state = {color:'green'}; } changeColor = (color)=>{ this.setState({color}); } childContextTypes = { color:PropTypes.string, changeColor:PropTypes.func } getChildContext(){ return {color:this.state.color,changeColor:this.changeColor}; } render(){ return ( <div style={{border:`5px solid ${this.state.color}`,padding:5}}> Page <Main/> </div> ) } } export default Page; ~~~