前言

组件之间为什么要通信?因为有依赖。

那么,作为React组件,怎么通信?

React官网说,

进行 父-子 通信,可以直接pass props。

进行 子-父 通信,往父组件传给子组件的函数注入参数。

对于没有 父-子 关系的组件间的通信,你可以设置你自己的全局事件系统。

详情见原文翻译文

那么,到底如何设置全局事件系统呢,React官网没讲。

但十美分的alloyteam的一篇文章却讲了。

该文指出,

  1. 在组件嵌套较深时,纯props通信不适用,因为要维护很长的通信链。
  2. 在组件很多时,定义很多公共变量也不方便。
  3. 使用Pub/Sub模式有助于简化多组件通信问题。

那么,市场上哪个Pub/Sub模式实现较好。

在此,我推荐Reflux

还要推荐一篇不错的Reflux的介绍和基本使用文章

Reflux认为React组件由Store,View,Actions组成。

View即HTML代码,它是由Store里面的原始数据经过运算得出。

Store里面除了原始数据,还有各种原始数据处理方法。

当View想改变Store的原始数据,可通过Actions。

组件之间的通信,就通过这些原始数据处理方法。

Reflux忽视组件之间的层级关系,不管父-子,子-父等等,皆可通过Store通信。

所以,重点来了(黑板敲三下),我们要理清组件之间的依赖关系。

那么,组件之间有哪些依赖关系呢。

请听下回分解。

可以剧透的是,

依赖关系无非三种,我依赖你,你依赖我,我们互相依赖,我们互相嘿嘿嘿。

其中,互相依赖这个场景较复杂,示例代码如下:

// actions1.js
module.exports = Reflux.createActions([
'getData',
'setData',
]); // PageDemo1.js
const reactMixin = require('react-mixin');
const Actions = require('./actions1');
const Store = require('./store1'); class Demo1 extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
click(){
Actions.setData("demo1的数据已被设置");
}
render() {
let t = this;
let data = Store.data;
if(data == null){
return null;
} return (
<div className="demo1">
<div>{data.name}</div>
<button onClick={t.click.bind(t)}>设置demo1的数据</button>
<button onClick={t.click.bind(t)}>设置demo2的数据</button>
</div>
);
} componentWillMount() {
} componentDidMount() {
Actions.getData();
} componentWillReceiveProps(nextProps) {
} shouldComponentUpdate(nextProps, nextState) {
return true;
} componentWillUpdate(nextProps, nextState) {
} componentDidUpdate(prevProps, prevState) {
} componentWillUnmount() {
}
} reactMixin.onClass(Demo1, Reflux.connect(Store));
module.exports = Demo1; // store1.js
const Actions = require('./actions');
module.exports = Reflux.createStore({
listenables: [Actions],
data: null,
onGetData:function() {
let t = this;
t.data = {};
t.data.name = "demo1";
t.updateComponent();
},
onSetData:function (name) {
let t = this;
t.data = {};
t.data.name = name;
t.updateComponent();
},
updateComponent: function() {
this.trigger(this.data);
}, getInitialState: function() {
return this.data;
}
}); // actions2.js
module.exports = Reflux.createActions([
'getData',
'setData',
"setDemo1Data"
]); // PageDemo2.js
const reactMixin = require('react-mixin');
const Actions = require('./actions2');
const Store = require('./store2');
const Demo1 = require('../demo1'); class Demo2 extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
click(){
Actions.setData("demo2的数据已被设置");
}
setDemo1Data(){
Actions.setDemo1Data("demo2设置demo1的数据");
}
render() {
let t = this;
let data = Store.data;
if(data == null){
return null;
} return (
<div className="demo2">
<div>{data.name}</div>
<div>
<button onClick={t.click.bind(t)} > 设置demo2的数据</button>
<button onClick={t.setDemo1Data.bind(t)} >设置demo1的数据</button>
</div>
<Demo1 />
</div>
);
} componentWillMount() {
} componentDidMount() {
Actions.getData();
} componentWillReceiveProps(nextProps) {
} shouldComponentUpdate(nextProps, nextState) {
return true;
} componentWillUpdate(nextProps, nextState) {
} componentDidUpdate(prevProps, prevState) {
} componentWillUnmount() {
}
} reactMixin.onClass(Demo2, Reflux.connect(Store));
ReactDOM.render(<Demo2/>, document.getElementById('App'));
module.exports = Demo2; // store2.js
const Actions = require('./actions2');
const demo1Store = require('../store1'); module.exports = Reflux.createStore({
listenables: [Actions],
data: null,
onGetData:function() {
let t = this;
t.data = {};
t.data.name = "demo2";
t.updateComponent();
},
onSetData:function (name) {
let t = this;
t.data.name = name;
t.updateComponent();
},
onSetDemo1Data:function(name) {
demo1Store.onSetData(name);
},
updateComponent: function() {
this.trigger(this.data);
}, getInitialState: function() {
return this.data;
}
});

如上上见,示例代码又多,又乱.

那么,有什么构建工具能快速搭建开发环境,运行示例代码。

在此,我推荐nowa

只要你机子上nodejs>=4.0 版本,npm>=3.0 版本,便可以使用nowa。

nowa 的诞生旨在解决以下痛点:

  • 每次下载或新建项目都要安装一坨开发用的依赖,而这些依赖绝大部分都是重复的,耗时又占空间(仅 babel 的一些插件就一百多兆);
  • 每个项目的构建任务配置在自己项目中维护,不方便统一维护和管理;
  • 构建配置对于很多新手用户来说还是太繁琐,迫切需要一个一站式的解决方案;
  • 项目模板的更新依赖于脚手架的发布,频繁更新用户体验不佳;
  • 希望有更流畅的开发体验;
  • 希望可以在一个地方找到所有常用的工具;
  • 希望能有一个便捷的远程调试方案;

    ……

好了,至于使用方法大家可进入官网查看,我掩面而逃~~~

使用reflux进行react组件之间的通信的更多相关文章

  1. react 组件之间的通信

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

  2. 关于react组件之间的通信

    才开始学react刚好到组件通信这一块,就简单的记录下组件间的通信方式:父到子:props.context,子到父:自定义事件.回调,兄弟组件:共父props传递.自定义事件import 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. Timusoj 1982. Electrification Plan

    http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...

  2. Eclipse版本android 65535解决方案(原理等同android studio现在的分包方式)

    由于工作的需要看了下Eclipse下android65535的解决方案,查了好多文档,真心的发自内心的说一句请不要再拷贝别人的博客了,害人,真害人. 接下来我说下我的实现方式,首先说下65535的最可 ...

  3. iOS 开发:利用第三方插件来安装CoCoapods

    引言:通过上一篇博客我们知道了怎么样去通过终端来安装CoCoapods,这一篇我们着重与用第三方插件来安装CoCoapods: 1. 首先在提下链接下载插件 https://github.com/ka ...

  4. Learning From Data 第一章总结

    之前上了台大的机器学习基石课程,里面用的教材是<Learning from data>,最近看了看觉得不错,打算深入看下去,内容上和台大的课程差不太多,但是有些点讲的更深入,想了解课程里面 ...

  5. Microsoft SQL Server 数据库服务器管理维护角色

    固定服务器角色: 按照从最低级别的角色(bulkadmin)到最高级别的角色(sysadmin)的顺序进行描述: Bulkadmin:这个服务器角色的成员可以运行BULK INSERT语句.这条语句允 ...

  6. js 一个自写的 监测类

    自从认识了jQuery后,很多页面加载入口,都放在document.ready里面.但是有时候这个觉得ready加载太慢, 这个[监测类 ]就开始产生了 效果类似这个. 每10毫秒检查一次,直到加载了 ...

  7. lua定义一个简单的类

    classA.lua: classA = { a = , b = , --__index = classA; }; classA.__index = classA; function classA:n ...

  8. List view优化

    ListView 针对每个item,要求 adapter "返回一个视图" (getView),也就是说ListView在开始绘制的时候,系统首先调用getCount()函数,根据 ...

  9. 巧用margin/padding的百分比值实现高度自适应(多用于占位,避免闪烁)

    本文依赖于一个基础却又容易混淆的css知识点:当margin/padding取形式为百分比的值时,无论是left/right,还是top/bottom,都是以父元素的width为参照物的!也许你会说, ...

  10. 关于onethink的迁移站点产生数据库错误

      为了支持国产,本人使用了onethink建立了一个自己的站点( 模板世界:www.templatesy.com ),使用至今,虽然碰到了重重困难,还有很多bug,但总算也勉强建了起来. 在近期的一 ...