美文网首页
React 父子、兄弟间通信

React 父子、兄弟间通信

作者: 小碗吃不了 | 来源:发表于2020-06-12 16:58 被阅读0次

与VUE比较

  • 思路相同、语法糖有出入

父子间

  • 父组件

     import React, { Component } from 'react'
     import Chuancan from "./Chuancan"
     class App extends Component {
     //js的构造函数,由于其他任何函数执行
     constructor(props) {
       super(props) //调用父类的构造函数,固定写法
       this.state = {
         inputValue: 'aa', 
         item: "0"
       }
     }
    render() {
     return (
       <div>
         {/* 子组件 ,content传参值、toFather接收事件*/}
         <Chuancan content={this.state.item} toFather={this.fatherEvent.bind(this)} />
       </div>
        )
     }
     fatherEvent(v) {
       console.log(v)
       }
     }
     export default App;
    
  • 子组件

    import React, { Component } from 'react';
    class Chuancan extends Component {
      state = {
        val: "222"
        }
      render() {
          return (<span onClick={this.childEvent.bind(this)}>父子传参{this.props.content}</span>);
      }
      childEvent() {
         {/* 接受和传参均需使用this.props*/}
          this.props.toFather(this.state.val);
        }
    }
    
    export default Chuancan;
    

兄弟间(改变兄弟组件的颜色,还是依靠父子通信)

  • 子组件一

     class Children1 extends Component{
        constructor(props){
         super(props);
      }
    handerClick(){
       this.props.changeChild2Color('skyblue') 
     }
     render(){
       return(
         <div>
             <div>children1</div>
             <button onClick={(e)=>{this.handerClick(e)}}>改变children2背景</button>
         </div>
       )
       }
     }
    
  • 子组件二

     class Children2 extends Component{
       constructor(props){
           super(props);
         }
     render(){
         return(
           <div style={{background:this.props.bgcolor}}>
                 <div>children2 背景色 {this.props.bgcolor}</div>
           </div>
         )
       }
     }  
    
  • 父组件

     class Father extends Component{
       constructor(props){
       super(props)
       this.state = {
         child2bgcolor:'pink'
       }
    }
     onchild2bgChange(color){
       this.setState({
         child2bgcolor:color
     })
     }
     render(){
       return(
         <div>
           <Children1 changeChild2Color={(color)=>{this.onchild2bgChange(color)}} />
           <Children2 bgcolor={this.state.child2bgcolor} />
         </div>
       )
       }
     }

相关文章

  • React 父子、兄弟间通信

    与VUE比较 思路相同、语法糖有出入 父子间 父组件 import React, { Component } fr...

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

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

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

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

  • 学习react一些资料链接等

    1,前淘宝团队,父子间通信:http://taobaofed.org/blog/2016/11/17/react-...

  • vue 组件通信方式 ,父子、隔代、兄弟 三类通信,六种方法

    Vue 组件间通信只要指以下 3 类通信:父子组件通信、隔代组件通信、兄弟组件通信,下面分别介绍每种通信方式且会说...

  • vue中的组件通信

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

  • React父子组件间的通信

    React数据流动是单向的,父组件向子组件的通信也是最常见的方式。父组件通过props向子组件传递需要的信息。子组...

  • react-父子组件间通信

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

  • react 组件通信

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

  • vue 组件之间通信

    一切从简,不用webpack。 1. props $emit 主要是父子之间通信,兄弟之间通信。 父子之间通信说明...

网友评论

      本文标题:React 父子、兄弟间通信

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