react的通信在详细讲解

本文探讨了React中父组件与子组件之间的数据传递和通信机制,包括属性、状态管理和事件处理。展示了如何通过props将数据从父组件传递到子组件,并通过回调函数实现子组件向父组件的数据回传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

父组件

import React,{ Component } from 'react'
import Son from './son.js'
export default class Shoukongzujian extends Component{
    constructor(props){
        super(props)
        this.state={
            arr:'12',
            arrs: [
                { id: 1, name: "废水"},
                { id: 2, name: "土壤" },
                { id: 3, name: "大气"},
            ],
            fathers: "我是父亲传过来的数据",
            number: 1,
            num: 0,
            title: "文本1"
        }
        this.tiji=this.tiji.bind(this)
        this.HelloDiv=React.createRef();
    }
    bandleSubmit=(e)=>{
        // 阻止默认行为
        e.preventDefault();
    }
    sub = (event)=>{
        const {arr}=this.state
        this.setState({
            arr: event.target.value
        },()=>console.log("-----arr----",arr))    
    }
    tiji(event){
        const {arr}=this.state
        this.setState({
            arr: event.target.value
        },()=>console.log("-----arr----",arr))
    }
    upinput = ()=>{
        console.log(this.state.arr, "------statearr----");
    }
    componentDidMount(){
        //  this.HelloDiv.current这个是获取它的元素
        this.HelloDiv.current.style.color="red"
        this.refs.divDemo.style.color="blue"
        console.log(this.state.arrs,'arrs');
    }
    par(na){
        this.setState({
            number: na
        })
    }
    // Uncaught TypeError: Cannot read property 'state' of undefined 没有使用箭头函数
    changexiugai(){
        /*
            Line 52:17:  'num' is assigned a value but never used  no-unused-var
            const { num } = this.state.num 这种是错误的
        */ 
        let { num } = this.state
        console.log("------num----", num);
        this.setState({
            num: num+10,
        })
    }
        chang=()=>{
        this.setState({
            title: "文本2"
        })
    }
    // shouldComponentUpdate判断是否更新dom state中属性的值
    shouldComponentUpdate(){
        console.log("------shouldComponentUpdate------");
        return true
    }
    render(){
        return(
            <div>
                <form onSubmit={this.bandleSubmit}>
                    <input type="text" style={{margin: "200px 0px 0px 0px"}} 
                    value={this.state.arr} onChange={this.sub}/><br/><br/>
                    <input type="password" ref={this.password}/><br/><br/>
                    <button ref={this.HelloDiv} onClick={this.upinput}>提交</button>
                    {/* 第二种方式获取 */}
                    <div ref="divDemo">ref的第二种写法</div>
                    {/* 
                        index.js:1 ./src/components/dis/dis.js
    Line 54:33:  Expected an assignment or function call and instead saw an expression 
    no-unused-expressions
    Search for the keywords to learn more about each error.     
                       {
                            this.state.arrs.map((item,index)=>{
                                <div key={item.id}>
                                    <li>{item.name}</li>
                                </div>
                            }                               
                        )
                        } 
                        要去掉花括号 

                    */}
                        {
                            this.state.arrs.map((item,index)=>(
                                <div key={item.id}>
                                    <li>{item.name}</li>
                                </div>
                            )                             
                        )
                    }
                </form>
                <h1>{this.state.number}</h1>
                {/* 子传父,是通过传在调用子组件中传自定义方法,在子组件中使用它传来的方法,把参数传过去,
                进行通信 */}
                <Son title={this.state.fathers} zdifa={this.par.bind(this)}
                 clickChanges={this.chang} ></Son>
                <p>{this.state.num}</p>
                <button onClick={ this.changexiugai.bind(this)}>点击修改</button>
                <p>{this.state.title}</p>
            </div>
        )
    }
}
// 数据比较多的时候使用非受控组件
/*
   refs使用的场景
   1.管理焦点,文本选择和媒体播发
   2.触发强制动画
   3.集成第三方DOM库
*/

子组件

import React,{ Component } from 'react'
export default class Son extends Component{
    constructor(props){
        super(props)
        this.state={
            des:'子元素传过来的数据',
            des1:'再次点击数据发生改变'

        }
    }
    ann = () =>{    
        this.props.zdifa(this.state.des)
    } 
    clickChange= ()=>{
        this.props.clickChanges()
    }
    render(){
        return (
            <div>
                我是子组件
                <li style={{listStyle:"none"}}>{this.props.title}</li>
                <button onClick={this.ann}>点击按钮</button>
                {/* 
                    在子组件中调用父组件自定义的方法
                    <button onClick={()=>{this.props.zdifa(this.state.des)}}>点击按钮在次改变数据</button>
                */}
                {/* 
                    通过儿子的事件,触发函数的执行
                */}
                  <button onClick={()=>{this.props.zdifa(this.state.des1)}}>再次点击</button>
                <button onClick={ this.clickChange }>修改title</button>
            </div>
        )
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值