• react的Provider,Consumer


    import React from 'react'
    import ReactDOM from "react-dom"
    const {Provider,Consumer} = React.createContext()
    class ContextDemo extends React.Component {
      state={
          newContext:'createContext'
      }
      render() {
        const {newContext} = this.state
        return (
            <Provider value={newContext}>
                <div>
                    <label>childContent</label>
                    <input type="text" value={newContext} onChange={e=>this.setState({newContext:e.target.value})}/>
                </div>
                <Son />
            </Provider>
        )
      }
    }
    class Son extends React.Component{
        render(){
            return <Consumer>
                {
                    (name)=>
                        <div style={{border:'1px solid red','60%',margin:'20px auto',textAlign:'center'}}>
                            <p>子组件获取到的内容:{name}</p>
                            <Grandson />
                        </div>
                    
                }
            
            </Consumer>
        }
    }
    class Grandson extends React.Component{
        render(){
            return <Consumer>
                {
                    (name)=>
                        <div style={{border:'1px solid red','60%',margin:'20px auto',textAlign:'center'}}>
                            <p>孙子组件获取到的内容:{name}</p>
                        </div>
                    
                }
            </Consumer>
        }
    }


    function render(){
      ReactDOM.render(
        <ContextDemo/>,
        document.getElementById('root')
      );
    }

    render();
  • 相关阅读:
    abap 调用Http --“Get”
    SAP-PI接口创建中的ABAP处理(不含PI配置)
    ALV 监听事件
    ABAP动态创建内表并展示--自撸版
    ABAP动态生成内表的三种方法
    SAP BP字段增强--付款条件检查
    SAP RFC上传接口(包含发布Webservice地址)
    C# 时间函数
    配置Excel的DCOM权限
    ORA-12571 : TNS : 包写入程序失败
  • 原文地址:https://www.cnblogs.com/MDGE/p/13701326.html
Copyright © 2020-2023  润新知