才开始学react刚好到组件通信这一块,就简单的记录下
组件间的通信方式:父到子:props、context,子到父:自定义事件、回调,兄弟组件:共父props传递、自定义事件
import React, { Component } from 'react';
import './ChatRoom.css';
import PropTypes from "prop-types";
import emitter from '../../utils/event' // 要安装event包 cnpm install events --save //event.js为:
//import { EventEmitter } from "events";
//export default new EventEmitter();
class Test extends Component {
constructor () {
super(...arguments)
this.state={
show: true
}
this.toggle = this.toggle.bind(this)
}
static childContextTypes = {
color: PropTypes.string
}
getChildContext () {
return {
color: 'red'
}
}
toggle () {
this.setState({
show: !this.state.show
})
console.log(this.state.show)
}
render () {
class Child1 extends Component {
constructor () {
super(...arguments)
console.log(this.props)
this.state = {
show: this.props.show
}
}
componentDidMount () { // 添加事件的监听
this.eventEmitter = emitter.addListener('testEvent',(msg) => {
console.log(msg)
})
}
componentWillUnmount () { // 组件销毁前清除事件监听
emitter.removeListener(this.eventEmitter)
}
render () {
class Child2 extends Component {
constructor () {
super(...arguments)
console.log(this.props)
this.send = this.send.bind(this)
}
static contextTypes = {
color:PropTypes.string
}
send () {
emitter.emit('testEvent','send event')
}
render () {
const style = {
color: this.context.color
}
return (
<div style={style}>子子组件
<button onClick={this.send}>自定义事件</button>
</div>
)
}
}
return (
<div>
<button onClick={this.props.func}>child1</button>
{this.state.show?<div>child1list</div>:null}
<Child2></Child2>
</div>
)
}
}
return (
<div>
<button onClick={this.toggle}>toggle子集列表</button>
<Child1 show={this.state.show} func={this.toggle}></Child1>
</div>
)
}
}
export defaultTest;
这些都是简单的组件通信、复杂的通信还是要用到redux,关于redux还没开始研究,后续会继续。

关于react组件之间的通信的更多相关文章

  1. 使用reflux进行react组件之间的通信

    前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...

  2. react 组件之间的通信

    react推崇的是单向数据流,自上而下进行数据的传递,但是由下而上或者不在一条数据流上的组件之间的通信就会变的复杂.解决通信问题的方法很多,如果只是父子级关系,父级可以将一个回调函数当作属性传递给子级 ...

  3. react组件之间的通信

    通过props传递 共同的数据放在父组件上, 特有的数据放在自己组件内部(state),通过props可以传递一般数据和函数数据, 只能一层一层传递 一般数据-->父组件传递数据给子组件--&g ...

  4. react native 之子组件和父组件之间的通信

    react native开发中,为了封装性经常需要自定义组件,这样就会出现父组件和子组件,那么怎么在父组件和子组件之间相互通信呢,也就是怎么在各自界面push和pop.传值. 父组件传递给子组件: 父 ...

  5. React 学习(六) ---- 父子组件之间的通信

    当有多个组件需要共享状态的时候,这就需要把状态放到这些组件共有的父组件中,相应地,这些组件就变成了子组件,从而涉及到父子组件之间的通信.父组件通过props 给子组件传递数据,子组件则是通过调用父组件 ...

  6. react组件之间的几种通信情况

    组件之间的几种通信情况 父组件向子组件通信 子组件向父组件通信 跨级组件通信 没有嵌套关系组件之间的通信 1,父组件向子组件传递 React数据流动是单向的,父组件向子组件通信也是最常见的;父组件通过 ...

  7. react第十七单元(redux和组件之间的通信,react-redux的相关api的用法)

    第十七单元(redux和组件之间的通信,react-redux的相关api的用法) #课程目标 什么是redux-redux react-redux的作用是什么 react-redux如何应用 #知识 ...

  8. React 组件之间通信 All in One

    React 组件之间通信 All in One 组件间通信 1. 父子组件之间通信 props 2. 兄弟组件之间通信 3. 跨多层级的组件之间通信 Context API https://react ...

  9. react 实现组件嵌套以及子组件与父组件之间的通信

    当子组件触发onChange事件时,实际调用的是父组件中的handelSelect函数,通俗来说就是父组件通过属性handleSelect实现与子组件之间的通信. 父组件:SignupForm 子组件 ...

随机推荐

  1. 手动编译Flume

    1.源码下载: 我用的是1.6版,因为加了kafka-sink,下载地址 http://www.apache.org/dyn/closer.cgi/flume/1.6.0/apache-flume-1 ...

  2. pg_restore数据库恢复指令

    pg_restore restores a PostgreSQL database from an archive created by pg_dump. Usage:  pg_restore [OP ...

  3. Django中的原子事务相关注意事项

    Django中的原子事务支持(transaction.atomic)方式函数装饰器或者with语句,这种方式特别是前者和spring里面的AOP事务支持方式基本等同,当然其实质方式都是原始的try.. ...

  4. 分布式文件系统MFS、Ceph、GlusterFS、Lustre的比较

    原文:http://blog.csdn.net/metaxen/article/details/7108958 MooseFS(MFS) Ceph GlusterFS Lustre Metadata ...

  5. Coursera-AndrewNg(吴恩达)机器学习笔记——第二周

    一.多变量线性回归问题(linear regression with multiple variables) 搭建环境OctaveWindows的安装包可由此链接获取:https://ftp.gnu. ...

  6. Angular4.x通过路由守卫进行路由重定向,实现根据条件跳转到相应的页面

    需求: 最近在做一个网上商城的项目,技术用的是Angular4.x.有一个很常见的需求是:用户在点击"我的"按钮时读取cookie,如果有数据,则跳转到个人信息页面,否则跳转到注册 ...

  7. DevOps之三 Git的安装与配置

    Centos7 安装Git 一.卸载Centos7 自带的git # git --version git version 1.8.3.1# whereis gitgit: /usr/bin/git / ...

  8. js基础--javaScript数据类型你都弄明白了吗?绝对干货

    欢迎访问我的个人博客:http://www.xiaolongwu.cn 数据类型的分类 JavaScript的数据类型分为两大类,基本数据类型和复杂数据类型. 基本数据类型:Null.Undefine ...

  9. vue技术分享-你可能不知道的7个秘密

    前言 本文是vue源码贡献值Chris Fritz在公共场合的一场分享,觉得分享里面有不少东西值得借鉴,虽然有些内容我在工作中也是这么做的,还是把大神的ppt在这里翻译一下,希望给朋友带来一些帮助. ...

  10. 深入理解Java NIO

    初识NIO: 在 JDK 1. 4 中 新 加入 了 NIO( New Input/ Output) 类, 引入了一种基于通道和缓冲区的 I/O 方式,它可以使用 Native 函数库直接分配堆外内存 ...