React学习:组件之间的关系、参数传递-学习笔记

React学习:组件之间的关系、参数传递-学习笔记

父到子传递参数

<!DOCTYPE html>
<html>
  <head>
    <title>组件传参(父-子)1</title>
    <meta charset="UTF-8" />
    <script src="js/react.development.js"></script>
    <script src="js/react-dom.development.js"></script>
    <script src="js/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
    //父组件
    class ParentList extends React.Component {
      constructor() {
        super();
        this.state = {
          name:'老张' ,
          msg:'我是爸爸'
        }; 
      }
      
      render() {
        let {name,msg} = this.state;
        return (
          <div>
            <h1>我是父组件</h1>
            <div>姓名:{name}</div>
            <div>描述:{msg}</div>
            <ChildList  {...this.state} />
          </div>
          )
      }
    };
    //子组件
    class ChildList extends React.Component {
      constructor(props) {    
        super();   
        this.state = {
          name:'child',
          msg:props.msg
        }; 
      }
      render() {
        let {name} = this.state;
        return (
          <div>
              <h1>我是子组件</h1>
              <div>子的姓名:{name}</div>
              <div>爸爸的姓名:{this.props.name}-{this.props.msg}</div>
          </div>
          )
      }
    };

    ReactDOM.render(
      <ParentList  />,
      document.getElementById('example')
    );
    </script>
  </body>
</html>
<!DOCTYPE html>
<html>
  <head>
    <title>组件传参(父-子)2</title>
    <meta charset="UTF-8" />
    <script src="js/react.development.js"></script>
    <script src="js/react-dom.development.js"></script>
    <script src="js/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
    //父组件
    class ParentList extends React.Component {
      constructor() {
        super();
        this.state = {
          name:'老张' ,
          msg:'我是爸爸'
        }; 
      }
      handleChange(e){
        this.setState({
          name:e.target.value
        })
      }
      handleParent(data){
        this.setState({
          name:data
        })
      }
      render() {
        let {name,msg} = this.state;
        return (
          <div>
            <h1>我是父组件</h1>
            <div>姓名:{name}</div> 
            <div>描述:{msg}</div>
            <input type="text" value={name} onChange={(event)=>this.handleChange(event)} />
            <ChildList  {...this.state} handleParent={this.handleParent.bind(this)}/>
          </div>
          )
      }
    };
    //子组件
    class ChildList extends React.Component {
      constructor(props) {    
        super();   
        this.state = {
          name:'child',
          parentName:props.name
        }; 
      }
      handleChange(e){
        //this.props.name = '11111'  //不能直接修改父
        this.setState({
          parentName:e.target.value
        });
        this.props.handleParent(e.target.value);

      }
      render() {
        let {name,parentName} = this.state;
        return (
          <div>
              <h1>我是子组件</h1>
              <div>子的姓名:{name}</div>
              <div>爸爸的姓名:{this.props.name}-{this.props.msg}</div>
              <input type="text" value={parentName} onChange={(event)=>this.handleChange(event)} />
          </div>
          )
      }
    };

    ReactDOM.render(
      <ParentList  />,
      document.getElementById('example')
    );
    </script>
  </body>
</html>

子-父

<!DOCTYPE html>
<html>
  <head>
    <title>组件传参(子-父)</title>
    <meta charset="UTF-8" />
    <script src="js/react.development.js"></script>
    <script src="js/react-dom.development.js"></script>
    <script src="js/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
    //父组件
    class ParentList extends React.Component {
      constructor() {
        super();
        this.state = {
          checked:true
        }; 
      }
      change(data,f1){
        console.log(data);
        f1();
        this.setState({
          checked:data
        })
      }
      render() {
        let {checked} = this.state;
        var isCheck = checked?'yes':'no';
        return (
          <div>
            <h1>我是父组件:{isCheck}</h1>
            <ChildList check={checked} parentChange={this.change.bind(this)}/>
          </div>
          )
      }
    };
    //子组件
    class ChildList extends React.Component {
      constructor(props) {    
        super();   
        this.state = {
          checked:props.check
        }; 
      }
      change(){
        var s = !this.state.checked;
        this.setState({
          checked:s
        });
        this.props.parentChange(s,this.f1);
      }
      f1(){
        console.log('f1f1f1')
      }
      render() {
        let {name} = this.state;
        return (
          <div>
              <div>我是子组件:
                {/*<input type='checkbox' 
                    checked={this.props.check}
                    onChange={()=>this.props.parentChange(false)}
              />*/}
              <input type='checkbox' 
                    checked={this.state.checked}
                    onChange={()=>this.change()}
                  />
              </div>
              
          </div>
          )
      }
    };

    ReactDOM.render(
      <ParentList  />,
      document.getElementById('example')
    );
    </script>
  </body>
</html>

父-孙

<!DOCTYPE html>
<html>
  <head>
    <title>组件传参(父-孙)</title>
    <meta charset="UTF-8" />
    <script src="js/react.development.js"></script>
    <script src="js/react-dom.development.js"></script>
    <script src="js/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
    //父组件
    class ParentList extends React.Component {
      constructor() {
        super();
        this.state = {
          color:'red'
        }; 
      }
      handleSelect(e){
        this.setState({
          color:e.target.value
        })
      }
      render() {
        var colorArr =['red','blue','green'];
        var {color} = this.state;
        return (
          <div>
            <select value={color} onChange={(event)=>this.handleSelect(event)}>
              {
                colorArr.map((item,index)=>{
                  return <option key={index}>{item}</option>
                })
              }
            </select>
            {
              this.props.data.map((item,i)=>{
                return <InputList key={i} obj={item} color2={color}  />
              })
            }
          </div>
          )
      }
    };
    //子组件
    class InputList extends React.Component {
      constructor(props) {    
        super();   
        this.state = {
        }; 
      }
      render() {
        return (
          <div>
              <input type='text' value={this.props.obj.value}/>
              <BtnList  color={this.props.color2} title={this.props.obj.title} />
          </div>
          )
      }
    };
     //子组件
     class BtnList extends React.Component {
      constructor(props) {    
        super();   
        this.state = {
        }; 
      }
      render() {
        return <button style={{'color':this.props.color}}>{this.props.title}</button>
      }
    };

    ReactDOM.render(
      <ParentList  data={[{id:1,title:'提交',value:'aaa'},{id:2,title:'编辑',value:'qqq'},{id:3,title:'删除',value:'www'}]}/>,
      document.getElementById('example')
    );
    </script>
  </body>
</html>

兄弟组件传参

<!DOCTYPE html>
<html>
  <head>
    <title>组件传参</title>
    <meta charset="UTF-8" />
    <script src="js/react.development.js"></script>
    <script src="js/react-dom.development.js"></script>
    <script src="js/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>

    <script type="text/babel">
    //父组件
    class ParentList extends React.Component {
      constructor() {
        super();
        this.state = {
          data:'12345'
        }; 
      }
      change(childA){
        this.setState({
          data:childA
        })
      }
      render() {
        let {data} = this.state;
        return (
          <div>
            <ChildListA change={this.change.bind(this)}/>
            <ChildListB {...this.state}/>
          </div>
          )
      }
    };
    //子组件
    class ChildListA extends React.Component {
      constructor(props) {    
        super();   
        this.state = {
          name:'AAAA'
        }; 
      }
      render() {
        return (
          <div>
              <div>我是子组件A:
                <button onClick={()=>this.props.change(this.state.name)}>按钮A</button>
              </div>
          </div>
          )
      }
    };
    class ChildListB extends React.Component {
      render() {
        return (
          <div>
              <div>我是子组件B:{this.props.data}</div> 
          </div>
          )
      }
    };

    ReactDOM.render(
      <ParentList  />,
      document.getElementById('example')
    );
    </script>
  </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值