美文网首页react
React 子组件给父组件传值、整个组件、方法

React 子组件给父组件传值、整个组件、方法

作者: Lia代码猪崽 | 来源:发表于2019-08-13 17:26 被阅读0次

一、准备工作

1.定义一个父组件,名字为Parent

/src/component/Parent.js

import React, {Component} from 'react'

export default class Parent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是父组件',
            msg: '父组件传值给子组件'
        }
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
            </div>
        )
    }
}

2.定义一个子组件 ,名字为Children

/src/component/Children.js

import React, {Component} from 'react'

export default class Children extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是子组件',
            msg: '子组件传值给父组件'
        }
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
            </div>
        )
    }
}
3.先在App.js里引入父组件Parent

/src/App.js

import React from 'react';
import Parent from './component/Parent'

function App() {
  return (
      <div>
          <Parent/>
      </div>
  );
}

export default App;

运行项目:


编译成功 界面如图所示,http://localhost:3000/
4.父组件Parent引入子组件Children
import React, {Component} from 'react'
import Children from './Children'

export default class Parent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是父组件',
            msg: '父组件传值给子组件'
        }
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
                <h3>我要引入子组件了:</h3>
                <hr/>
                <Children/>
            </div>
        )
    }
}
已成功引入子组件

二、子组件Children传值(msg)给父组件Parent

子组件传值给父组件的步骤:

  1. 父组件在调用子组件时,传入一整个组件给子组件<Children parent={ this } />
  2. 父组件中定义一个方法getChildrenMsg(resulet, msg),用来获取子组件传来的值以及执行其他操作
  3. 子组件在通过this.props来获取到一整个组件this.props.parent或者this.props[parent]
  4. 子组件调用父组件步骤2里定义的方法,通过bind绑定传值

Parent:

import React, {Component} from 'react'
import Children from './Children'

export default class Parent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是父组件',
            msg: '父组件传值给子组件',
            childrenMsg: ''
        }
    }

    getChildrenMsg = (result, msg) => {
        // console.log(result, msg)
        // 很奇怪这里的result就是子组件那bind的第一个参数this,msg是第二个参数
        this.setState({
            childrenMsg: msg
        })
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
                <h3>子组件传来的值为:{ this.state.childrenMsg }</h3>
                <h3>我要引入子组件了:</h3>
                <hr/>
                <Children parent={ this } />
            </div>
        )
    }
}

Children:

import React, {Component} from 'react'

export default class Children extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是子组件',
            msg: '子组件传值给父组件'
        }
    }

    toParent = () => {
        // console.log(this.props.parent.getChildrenMsg.bind(this, this.state.msg))
        this.props.parent.getChildrenMsg(this, this.state.msg)
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
                <button onClick={ this.toParent }>子组件传入给父组件</button>
            </div>
        )
    }
}

三、子组件Children给父组件Parent传一整个组件(父组件获取整个子组件)

子组件给父组件传一整个组件(父组件获取整个子组件)的步骤:

  1. 父组件在调用子组件时,通过ref属性,拿到整个子组件<Children ref='children'>
  2. 父组件中通过this.refs.children或者this.refs[children]获取到一整个子组件实例(注意,要在DOM加载后才能获取)

Parent:

import React, {Component} from 'react'
import Children from './Children'

export default class Parent extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是父组件',
            msg: '父组件传值给子组件',
            childrenMsg: ''
        }
    }

    getChildrenMsg = () => {
        this.setState({
            childrenMsg: this.refs['children'].state.msg
        })
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
                <button onClick={ this.getChildrenMsg }>获取更新子组件的msg值</button>
                <h3>子组件传来的值为:{ this.state.childrenMsg }</h3>
                <h3>我要引入子组件了:</h3>
                <hr/>
                <Children ref="children" />
            </div>
        )
    }
}

Children:

import React, {Component} from 'react'

export default class Children extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '我是子组件',
            msg: '子组件传值给父组件'
        }
    }

    render() {
        return (
            <div>
                <h2>{ this.state.name }</h2>
                <h3>子组件的msg为:{ this.state.msg }</h3>
            </div>
        )
    }
}

初始页面,点击按钮
点击按钮后

四、子组件Children给父组件Parent传方法

在第三点获取到整个组件的前提上,再获取方法,所以不详细讲了。

相关文章

  • react-父子组件间通信

    React-父子组件间通信 父组件传 值 给子组件, 传 方法 给子组件 子组件接收 值 ,触发父组件的 方法

  • Vue_组件间传值

    1、父组件传值给子组件2、子组件传值给父组件 1、父组件传值给子组件 2、子组件传值给父组件

  • react-native 父子组件之间传值

    1.父组件传值给子组件 父组件给子组件传值 react 和vue都是很相似的 , 很简单 1.父组件引入子组件im...

  • 2018-09-05

    组件传值问题 父组件给子组件传值应该使用props。子组件要给父组件传值,需要调用父组件传递的方法。props传值...

  • vue2.0 父子组件传值

    父组件传值给子组件 子组件 子组件通过 props 接收值 父组件 父组件通过标签属性传值 子组件传值给父组件 子...

  • VUE组件之间传值

    1.父组件传值给子组件 父组件image.png 子组件image.png 2.子组件传值给父组件 方法1: 子组...

  • vue组件间参数与事件传递

    父组件向子组件传值 以及父组件调用子组件方法 子组件向父组件传值 以及子组件触发调用父组件方法

  • react子组件、父组件相互传值

    子组件传图片路径imgUrl值给父组件,父组件传imgaddr值给子组件子组件: 父组件:

  • 子组件向父组件传值

    子组件向父组件传值 思路:子组件向父组件传值,可以通过调用父组件的方法,来变相的传值 父组件向子组件传递方法 父元...

  • Angular 8 父子组件之间通信(简单易懂)

    父组件给自组件传值 - @input 父组件不仅可以给子组件传递简单的数据,还可以把自己的方法以及整个父组件传给子...

网友评论

    本文标题:React 子组件给父组件传值、整个组件、方法

    本文链接:https://www.haomeiwen.com/subject/ezebjctx.html