zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    maven settings
    java.util.Base64
    Centos 7 下 LAMP 部署
    Cisco N3K VPC+HSRP+ospf 配置
    centos 7 下多网卡绑定+ vlan 网卡配置
    centos 7 下 cobbler 安装
    hive0.12 rcfile gzip 测试
    Hive内部表外部表转化分析(装)
    hadoop2.2.0 + hbase 0.94 + hive 0.12 配置记录
    hbase 问题记录
  • 原文地址:https://www.cnblogs.com/MDGE/p/13701326.html
Copyright © 2011-2022 走看看