Reflux之Store
Reflux中的Store既是一个listener(既有对action的监听,又有对store的监听)同时又是一个publisher.
一、监听单个action
const Reflux = require('reflux');
const action = Reflux.createAction();
const store = Reflux.createStore({
init() {
this.data = { num:0 };
// store监听action
this.listenTo(action, function(){
this.data.num++;
this.trigger(this.data);
}.bind(this))
}
})
// 监听store触发
store.listen(data => console.log(data));
// 触发action
action.trigger('in action');
action();
action();
action();
action();
action();
注意: 1. store.listen方法对store自身trigger进行监听。
2. store.listenTo对其他可监听对象进行监听。
二、同时监听多个actions
const Reflux = require('reflux');
// const actions = Reflux.createActions(['action1', 'action2']);
const actions = Reflux.createActions({
action1: {
asyncResult: true
},
action2: {
asyncResult: true
}
});
const store = Reflux.createStore({
listenables: actions,
// init() {
// this.listenToMany(actions)
// },
action1 () {
console.log('func in action1');
},
onAction1Completed () {
console.log('action1 completed')
},
onAction2() {
console.log('func in action2')
}
})
actions.action1();
actions.action2();
actions.action1.completed();
这里,在createStore中使用listenables属性,或者在init函数中使用listenToMany都可以实现对多个action的监听。使用这种写法对应的callback函数,可以与每个action同名,如action1;也可以使用on+Action,如onAction2。如果使用asyncResult属性定义action,默认下面有completed和failed两个children.
三、 react与Reflux结合demo import { createAction, createStore } from 'reflux';
import React from 'react'; const action = createAction();
const store = createStore({
init() {
this.data = {num: 0};
this.listenTo(action, this.onClick);
},
onClick () {
this.data.num ++;
this.trigger(this.data);
}
})
// React UI
class ContainerUI extends React.Component {
constructor (props) {
super(props);
this.state = {
num: 0
}
}
componentDidMount () {
// 生成关闭函数
this.unmount = store.listen(data => {
this.setState({
num: data.num
})
})
}
componentWillUnmont () {
//调用关闭函数
this.unmount();
}
render () {
return (
<div>
{ this.state.num }
<button onClick={action}>自增</button>
</div>
)
}
}
export default ContainerUI;
Reflux之Store的更多相关文章
- react+reflux入门教程
为了简化react的flux带来的冗余操作,社区的同仁们给我们带来了很多优秀的轮子,诸如redux,reflux等.今天我们就通过逐行讲解代码实例的方法,感受一番reflux的设计之美. 例子 这个例 ...
- 使用reflux进行react组件之间的通信
前言 组件之间为什么要通信?因为有依赖. 那么,作为React组件,怎么通信? React官网说, 进行 父-子 通信,可以直接pass props. 进行 子-父 通信,往父组件传给子组件的函数注入 ...
- react 之 reflux 填坑
注意:老铁些,在看这篇文章的之前,最好了解一下react 的全局状态管理库哦,不然可能会坐飞机. ^_^ React 之reflux (它是一个功能模块,需要安装引入): import Reflux ...
- 【原创】ReFlux细说
ReFlux细说 Flux作为一种应用架构(application architecture)或是设计模式(pattern),阐述的是单向数据流(a unidirectional data flow) ...
- React + Reflux
React + Reflux 渲染性能优化原理 作者:ManfredHu 链接:http://www.manfredhu.com/2016/11/08/23-reactRenderingPrinc ...
- Reflux 使用教程
Reflux是根据React的flux创建的单向数据流类库.Reflux的单向数据流模式主要由actions和stores组成.例如,当组件list新增item时,会调用actions的某个方法(如a ...
- Reflux中文教程——概览
翻译自github上的reflux项目,链接:https://github.com/reflux/refluxjs 〇.安装及引入 安装: npm install reflux 引入: var Ref ...
- Reflux系列01:异步操作经验小结
写在前面 在实际项目中,应用往往充斥着大量的异步操作,如ajax请求,定时器等.一旦应用涉及异步操作,代码便会变得复杂起来.在flux体系中,让人困惑的往往有几点: 异步操作应该在actions还是s ...
- React + Reflux 渲染性能优化原理
作者:ManfredHu 链接:http://www.manfredhu.com/2016/11/08/23-reactRenderingPrinciple 声明:版权所有,转载请保留本段信息,否则请 ...
随机推荐
- TVP专家眼中的云开发:定是未来,尚不完美
TVP专家眼中的云开发:定是未来,尚不完美 C++之父 Bjarne曾说,"世界上只有两种编程语言,一种被人骂,一种没人用".这句玩笑话道出了软件开发行业的真谛,不怕被人吐槽,就怕 ...
- scala教程之:可见性规则
文章目录 public Protected private scoped private 和 scoped protected 和java很类似,scala也有自己的可见性规则,不同的是scala只有 ...
- 【高并发】由InterruptedException异常引发的思考
写在前面 InterruptedException异常可能没你想的那么简单! 前言 当我们在调用Java对象的wait()方法或者线程的sleep()方法时,需要捕获并处理InterruptedExc ...
- CloudCC CRM探讨:精细流程管理与员工悟性培养
很多企业主招聘时更喜欢专业的销售,来给他们创造价值.老板不愿意花时间在"磨刀上",而喜欢员工一来就"砍柴".即使是建立培训机制,仍然很大程度依赖于员工自己的悟性 ...
- SaltStack数据系统之Grains、Pillar
SaltStack数据系统之Grains.Pillar 1.什么是Grains? Grains是saltstack的组件,用于收集salt-minion在启动时候的信息,又称为静态信息.Grains是 ...
- HDU 1421 搬寝室 解题报告(超详细)
**搬寝室 Time Limit: 2000/1000 MS Memory Limit: 65536/32768 K Problem Description 搬寝室是很累的,xhd深有体会.时间追述2 ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)
ACM思维题训练集合 The Little Elephant has got a problem - somebody has been touching his sorted by non-decr ...
- 洛谷P1217 回文质数
题目描述 因为 151 既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围 [a,b] (5 \le a < b \le 100,000 ...
- Spring源码阅读 之 配置的加载(希望有喜欢源码的朋友一起交流)
想写Spring的源码方面的东西想了好久了,之前花了一段时间学习了SpringCloud,现在总算对SpringCloud有了一个大概的了解,从今天开始好好读一篇Spring的源码,结合书本跟网上的一 ...