美文网首页
React父子组件通信问题

React父子组件通信问题

作者: 666同学 | 来源:发表于2020-03-18 22:48 被阅读0次

1、传值问题

    1-1父组件向子组件传值

通过props传值

    1-2子组件向父组件传值

首先想父组件可以通过props传值,也可通过props传方法。

父组件通过props向子组件传方法,子组件接受父组件的方法,通过父组件的方法传值并调用,父组件就能接收到子组件的传值了

//父组件

import React, { Component } from "react";

import Child from "./Child";

class Dad extends Component {

    constructor(props) {

        super(props);

        this.state = {

            txt:"我是尼爸爸"

        }

    }

    fn=(data)=>{

        this.setState({

            txt:data

        })

    }

    render() {

        return (

            <div>

                <Child cfn={this.fn}></Child>

                <p>{this.state.txt}</p>

            </div>

        )

    }

}

export default Dad;

//子组件

import React, { Component } from "react";

class Child extends Component {

    constructor(props){

        super(props);

    }

    fn=(data)=>{

        this.props.cfn(data);

    }

    render() {

        return (

          <div>

            //通过事件进行传值

              <button onClick={()=>{this.fn("我是儿子")}}>子组件向父组件传值</button>

          </div>

        )

    }

}

export default Child;

2、相互调用方法(子组件调用父组件的方法,父组件调用子组件的方法)

2-1子组件调用父组件的方法

父组件通过props向子组件传方法,子组件通过this.props.function()直接调用

2-2父组件调用子组件的方法

首先父组件需要获取到子组件的方法,其思路和获取子组件的值一样。

先向子组件传方法,子组件通过父组件传递的方法向父组件传递方法,父组件获取到子组件的方法直接调用。

//父组件

import React, { Component } from "react";

import Child from "./Child";

class Dad extends Component {

    constructor(props) {

        super(props);

        this.state = {

            arr:["暴富","暴瘦"],

            txt:"我是尼爸爸"

        }

    }

    onRef=(ref)=>{

        this.Child=ref;

    }

    click=()=>{

        this.Child.childFn();

    }

    render() {

        return (

            <div>

                <Child onRef={this.onRef}></Child>

                <button onClick={this.click}>父组件调用子组件中的方法</button>

            </div>

        )

    }

}

export default Dad;

//子组件

import React, { Component } from "react";

class Child extends Component {

    constructor(props){

        super(props);

    }

    componentDidMount() {

        this.props.onRef(this)

    }

    childFn=()=>{

        console.log("我是子组件中的方法")

    }

    render() {

        return (

          <div>

          </div>

        )

    }

}

export default Child;

相关文章

  • react 组件通信

    概述 react中的组件通信问题,根据层级关系总共有四种类型的组件通信:父子组件、爷孙组件、兄弟组件和任意组件。前...

  • React父子组件间通信的实现方式

    React学习笔记之父子组件间通信的实现:今天弄清楚了父子组件间通信是怎么实现的。父组件向子组件通信是通过向子组件...

  • 「React Native」Event Bus,消息总线

    (一)父子间组件通信:   一般使用props,回调函数进行通信。(二)跨组件之间通信:  (1)React Na...

  • React入门基础知识总结

    1.React组件 function组件, class组件,来自ES6的class语法, 2. 父子组件通信 父组...

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • React02-组件通信

    React父子组件之间如何通信父组件传一个函数给子组件,子组件在适当的时候调用这个函数React爷孙组件之间如何通...

  • React 父子组件通信

    通讯是单向的,数据必须是由一方传到另一方。 1.父组件与子组件间的通信。 在 React 中,父组件可以向子组件通...

  • react父子组件通信

    父组件通过props 给子组件传递数据,子组件则是通过调用父组件传给它的函数给父组件传递数据。

  • react父子组件通信

    父组件向子组件通信 回调函数 直接把函数传到组件里面,然后组件里面调用this.props.goDetail函数来...

  • react 父子组件通信

    1. 子组件拿到父组件数据 在父组件中定义一个函数,将其传递到子组件中,子组件调用这个回调函数就可以拿到父组件中的...

网友评论

      本文标题:React父子组件通信问题

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