react父子组件传值

Posted zdping

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react父子组件传值相关的知识,希望对你有一定的参考价值。

react均是以组件构成,那父子组件传值就很重要了

父组件传值给子组件,不仅可以传值,传事件,还可以传实例

1、父组件传值给子组件

传值

父组件:

import React from ‘react‘;
import Children from ‘./Children‘;

class Father extends React.Component {
    constructor(props) {
        super(props);
        this.state = {  
            msg:‘父组件的msg‘
        };
    } 
    render() {
        return (
            <div>
                <div>我是父组件</div>
                <Children msg={this.state.msg}/>
            </div>
        );
    }
}

export default Father;

子组件:

import React from ‘react‘;

class Children extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            msg:‘子组件‘
        };
    } 
    render() {
        return (
            <div>
                <div>子组件</div>
                <div>{this.props.msg}</div>
            </div>
        );
    }
}

export default Children;

效果如下:

技术分享图片

传事件:

父组件:

import React from ‘react‘;
import Children from ‘./Children‘;

class Father extends React.Component {
    constructor(props) {
        super(props);
        this.state = {  
            msg:‘父组件的msg‘
        };
    } 
    run=()=>{
        alert(‘给子组件获取事件‘)
    }
    render() {
        return (
            <div>
                <div>我是父组件</div>
                <Children msg={this.run}/>
            </div>
        );
    }
}

export default Father;

子组件:

import React from ‘react‘;

class Children extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            msg:‘子组件‘
        };
    } 
    render() {
        return (
            <div>
                <div>子组件</div>
                <div>{this.state.msg}</div>
                <button onClick={this.props.msg}>获取父组件的事件</button>
            </div>
        );
    }
}

export default Children;

效果如下:

技术分享图片

获取父组件的整个实例

父组件:

import React from ‘react‘;
import Children from ‘./Children‘;

class Father extends React.Component {
    constructor(props) {
        super(props);
        this.state = {  
            msg:‘父组件的msg‘
        };
    } 
    run=()=>{
        alert(‘给子组件获取事件‘)
    }
    render() {
        return (
            <div>
                <div>我是父组件</div>
                <Children msg={this}/>
            </div>
        );
    }
}

export default Father;

子组件:

import React from ‘react‘;

class Children extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            msg:‘子组件‘
        };
    } 
    render() {
        return (
            <div>
                <div>子组件</div>
                <div>{this.state.msg}</div>
                <div>获取父组件的值{this.props.msg.state.msg}</div>
                <button onClick={this.props.msg.run}>获取父组件的事件</button>
            </div>
        );
    }
}

export default Children;

效果如下:

技术分享图片

2、子组件传值给父组件,通过ref传值

父组件:

import React from ‘react‘;
import Children from ‘./Children‘;

class Father extends React.Component {
    constructor(props) {
        super(props);
        this.state = {  
            msg:‘父组件的msg‘
        };
    }  
    getData=()=>{
        this.setState({
            msg:this.refs.children.state.msg
        })
    }
    render() {
        return (
            <div>
                <div>我是父组件</div>
                <div>{this.state.msg}</div>
                <Children ref=‘children‘/>
                <button onClick={this.getData}>获取子组件</button>
            </div>
        );
    }
}

export default Father;

子组件:

import React from ‘react‘;

class Children extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            msg:‘子组件‘
        };
    } 
    render() {
        return (
            <div>
                <div>子组件</div>
            </div>
        );
    }
}

export default Children;

效果如下:

技术分享图片技术分享图片

 

以上是关于react父子组件传值的主要内容,如果未能解决你的问题,请参考以下文章

React 父子组件传值

react父子组件传值

react父子组件之间传值

React 父子组件和非父子组件传值

react父子组件传值

react基础总结篇1,定义组件实现父子组件传值