react的组件通信

react的组件通信

1、父组件传子组件

import React, {Component} from 'react'
class Father extends Component{ render() { return ( <div> <Child msg="我是父组件中的数据:father-data"/> </div> ) } } class Child extends Component{ constructor(props) { super(props) this.state = { message: props.msg } } render() { return ( <div> <div>父组件传过来的数据是:{this.state.message}</div> </div> ) } } export default Father

父组件在调用的子组件上定义一个属性msg,属性的值就是需要传递的数据。在子组件中,通过props.msg获取数据。

2、子组件传父组件

import React, { Component } from 'react'
class Child extends Component {
    constructor(props) {
        super(props)
    }
    input = (e) => {
        console.log(e.target.value);
        if (e.keyCode == 13) {
            this.props.send(e.target.value)
        }
    }
    render() {
        return (
            <div>
                <input type="text" placeholder='please input' onKeyUp={this.input} />
            </div>
        )
    }
}
class Father extends Component {
    state = {
        inputVal: "123"
    }
    getData = (val) => {
        this.setState({
            inputVal: val
        })
    }
    render() {
        return (
            <div>
                <Child send={this.getData} />
                子元素传过来的值:{this.state.inputVal}
            </div>
        )
    }
}
export default Father

子组件传父组件是通过在子组件中使用props调用父组件中定义的方法(函数)并将自己的数据传递过去。

如上所示,父组件在调用的子组件上定义了send方法用于获取子组件传过来的数据,子组件中调用父组件中的send方法将input的值传过去。

注意:若定义的函数不是箭头函数,则需要在调用的地方使用bind绑定当前this。如this.getdata.bind(this)

3、兄弟组件通信

import React, {Component} from 'react'
class A extends Component{
    state = {
        inputVal: "module A default value"
    }
    handleChange = (e) => {
        this.setState ({
            inputVal: e.target.value
        })
    }
    sendData = () = {
        const { sendFn } = this.props
        sendFn(this.state.inputVal)
    }
    render() {
        return (
            <div>
                <input type="text" value={this.state.inputVal} onChange={this.handleChange}/>
                <button onClick={this.sendData}>send</button>
            </div>
        )
    }
}
class B extends Component{
    const { sendVal } = this.props
    render() {
        return (
            <div>
                <p>B组件接收到的值是:{sendVal}</p>
            </div>
        )
    }
}
class Public extends Component{
    state = {
        inputVal: "module Public default value"
    }
    handleUpdate= (inputVal) => {
        this.setState({
            inputVal: inputVal
        })
    }
    render() {
        return (
            <div>
                <A sendFn={this.handleUpdate}></A>
                <B sendValue={this.state.inputVal}></B>
            </div>
        )
    }
}
export default Public

兄弟组件传值(A传B)需要使用公共组件(Public)进行过渡,即A传Public、Public传B。

A组件通过监听input框输入的值,然后点击按钮,在按钮事件中会调用公共组件中的更新视图(handleUpdate)的方法,将文本框的值作为参数传进去,然后公共组件就获取到A组件的值,然后将公共组件的值传给B组件,B组件再去就接收就能获取到公共组件的值,这样,也就获取到A组件传过来的值。

React组件通信可以通过props属性进行传递数据。子组件可以通过props接收父组件传递的数据,并进行相应的操作。在函数式组件中,可以直接通过props对象来获取父组件传递的数据。在类组件中,需要使用this.props来获取父组件传递的数据。\[1\] 另外,子组件也可以通过调用父组件传递过来的函数来实现子传父的通信。子组件可以将需要传递的数据作为函数的实参传入,并在子组件内部调用该函数。这样就可以将子组件的数据传递给父组件进行处理。\[3\] 总结起来,React组件通信可以通过props属性进行父传子和子传父的数据传递和操作。父组件通过props将数据传递给子组件,子组件通过props接收父组件传递的数据。子组件可以通过调用父组件传递的函数来将子组件的数据传递给父组件进行处理。\[1\]\[3\] #### 引用[.reference_title] - *1* *3* [React组件通信](https://blog.csdn.net/weixin_52148548/article/details/125287315)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [react组件通信](https://blog.csdn.net/weixin_39818813/article/details/125737028)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值