React中的组件:父子组件传值

React中的组件: 解决html 标签构建应用的不足。

使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入。

父子组件:组件的相互调用中,我们把调用者称为父组件,被调用者称为子组件

父子组件传值:

父组件给子组件传值 

	    1.在调用子组件的时候定义一个属性  <Header msg='首页'></Header>

	    2.子组件里面 this.props.msg          


说明:父组件不仅可以给子组件传值,还可以给子组件传方法,以及把整个父组件传给子组件。



父组件主动获取子组件的数据

    1、调用子组件的时候指定ref的值   <Header ref='header'></Header>      
    
    2、通过this.refs.header  获取整个子组件实例

代码:

import React, {Component} from 'react';

class Sonc extends Component {
    constructor(props) {
        super(props);
        this.state = { 
            msg2:'i am son c',
            givefa:'it is sonc ,it will give fatherc'
         };
    }
    render() {
        return (
            <div> 
                {/* {this.state.msg} 
                {this.state.givefa} */}
                {/* {this.props.title}
               <button onClick={this.props.run}>点击执行父组件的run方法</button>
               <button onClick={this.props.allf.getData}>点击执行父组件的run方法</button>
               <button onClick={this.props.allf.getChildData.bind(this,'我是子组件的数据')}>点击执行子组件给父组件传值</button> */}
               <hr /> 以下是父组件主动获取子组件的数据
              
            </div>
        );
    }
}

export default Sonc;

代码2:

import React, {Component} from 'react';
import Sonc from './Sonc';

class Fatherc extends Component {
    constructor(props) {
        super(props);
        this.state = { 
                msg1:'i am fatherc',
                title:"it is father's title"
               
         };
    }
    run=()=>{
        alert(" i am run ff of fuzujian")
    }
    getData=()=>{
        alert(this.state.msg1)
    }
    getChildData=(result)=>{
        alert(result);

        this.setState({
            msg1:result
        })
    }
    getSonc=()=>{
        alert(this.refs.sonc.state.msg2)
    }
    render() {
        return (
            <div>
                {this.state.msg1}
                <hr />
                {/* <Sonc  title={this.state.title}  run={this.run} allf={this}/>
                 {this.state.msg1} */}
                 <button onClick={this.getSonc}>获取整个底部组件</button>
                <Sonc ref='sonc' />
            </div>
        );
    }
}

export default Fatherc;
import React, {Component} from 'react';
import Sonc from './Sonc';

class Fatherc02 extends Component {
    constructor(props) {
        super(props);
        this.state = { 
                msg1:'i am fatherc02',
                title:"it is fatherc02's title"
               
         };
    }
    render() {
        return (
            <div>
                {this.state.msg1}
                <hr />
                <Sonc  title={this.state.title} />

            </div>
        );
    }
}

export default Fatherc02;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值