重温react-04

兄弟组件之间通信

兄弟1

import React, { Component } from 'react'
import pubsub from './pubsub'
export default class learnReact01 extends Component {
  render() {
    return (
      <div>我是兄弟1
        <button onClick={this.clickMessage}>向兄弟2发信息</button>

      </div>
    )
  }
  clickMessage=()=>{
    pubsub.publish('message','6666')
  }
}

兄弟2

import React, { Component } from 'react'
import pubsub from './pubsub'
export default class learnReact01 extends Component {
  state = {
    msg: ""
  }
  render() {
    return (
      <div>我是兄弟2
        <div>
          {this.state.msg}
        </div>
      </div>
    )
  }
  componentDidMount() {
    pubsub.subscribe('message', (data) => {
      console.log(data)
      this.setState({
        msg: data
      })
    })
  }
}

react中操纵dom和ref

import React, { Component,createRef } from 'react'
import LearnReact from './learnReact04'
export default class learnReact03 extends Component {
    constructor(props) {
        super(props)
        this.state = {
            name: '张三'
        }
        this.divRef = createRef(null)
        this.learnReactRef = createRef(null)
        this.buttonRef = createRef(null)
    }
  render() {
    return (
      <div>
        <div ref={this.divRef}>我是父组件</div>
        <LearnReact ref={this.learnReactRef} />
        <button ref={this.buttonRef} onClick={this.onclickThisMessage}>点击</button>
      </div>
    )
  }
  componentDidMount() {
      console.log(this.divRef.current);
      console.log(this.learnReactRef.current);
      console.log(this.buttonRef.current);
  }
  onclickThisMessage=()=>{
    this.learnReactRef.current.onclick()
  }
}

相关推荐

  1. 重温react-04

    2024-06-13 17:56:04       6 阅读
  2. 重温react-03

    2024-06-13 17:56:04       5 阅读
  3. <span style='color:red;'>react</span><span style='color:red;'>03</span>

    react03

    2024-06-13 17:56:04      17 阅读
  4. React04-关于React Props的实践

    2024-06-13 17:56:04       32 阅读
  5. 重新理解React-hook

    2024-06-13 17:56:04       9 阅读
  6. day04--react中批量传递props

    2024-06-13 17:56:04       13 阅读
  7. day04--react中state的简化

    2024-06-13 17:56:04       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 17:56:04       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 17:56:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 17:56:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 17:56:04       18 阅读

热门阅读

  1. 视图、触发器、存储过程、函数语法

    2024-06-13 17:56:04       6 阅读
  2. FAQ for ASAN on Android10/Android11/Android12/8155/8295

    2024-06-13 17:56:04       7 阅读
  3. 网页自动化工具入门篇之常用自动化工具

    2024-06-13 17:56:04       10 阅读
  4. TDengine数据迁移

    2024-06-13 17:56:04       6 阅读
  5. recover pdb

    2024-06-13 17:56:04       5 阅读
  6. NEWWAY推力轴承有何不同之处?

    2024-06-13 17:56:04       8 阅读
  7. 从Android刷机包提取System和Framework

    2024-06-13 17:56:04       6 阅读
  8. 正则表达式

    2024-06-13 17:56:04       5 阅读
  9. Kolmogorov-Arnold Networks (KANs)

    2024-06-13 17:56:04       7 阅读
  10. shell脚本--基础版本

    2024-06-13 17:56:04       4 阅读