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 声明:版权所有,转载请保留本段信息,否则请 ...
随机推荐
- 集合-ArrayList 源码解析
ArrayList是一种以数组实现的List,与数组相比,它具有动态扩展的能力,因此也可称之为动态数组. 类图 ArrayList实现了List, RandomAccess, Cloneable, j ...
- ThinkPHP框架初步掌握
为了帮老师用ThinkSNS二次开发一个微博系统,专门花了几天学习ThinkPHP框架,现在将一些ThinkPHP入门知识作以记录. 首先声明: 本文不是完全教程,只是将开发中碰到的问题作以总结,如果 ...
- event兼容性解决
event出现undefind错误 解决方法: $('#normalImgDiv').mousemove(function (e) { var e = window.event || e; var p ...
- itchat学习
itchat是一个开源的微信个人号接口,可以很方便的使用python调用微信. 教程如下:https://itchat.readthedocs.io/zh/latest/ 简单试玩了一下,觉得还挺有趣 ...
- linux服务器(CentOS)一键安装express框架
express框架需要nodejs环境支持,没有安装node.js环境的同学可以参照下面这篇博客 linux服务器安装配置Node.js 好了,言归正传.先使用xshell或者其它软件连接我们的服务器 ...
- 03 Django下载和使用 三板斧httpresponse render redirect
简介 是一个为完美主义者设计的web框架 The web framework for perfectionists with deadlines. Django可以使你能够用更少的代码,更加轻松且快速 ...
- session与cookie的浅谈
cookie的用途: 当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为你浏览某个网页的时候,WEB 服务 ...
- Spring Cloud学习 之 Spring Cloud Ribbon 重试机制及超时设置不生效
今天测了一下Ribbon的重试跟超时机制,发现进行的全局超时配置一直不生效,配置如下: ribbon: #单位ms,请求连接的超时时间,默认1000 ConnectTimeout: 500 #单位ms ...
- php时间输出结果减去一分钟
如: <?=date("m-d H:i",strtotime($rs["kgtime"]));?> 假设结果是09-03-23:30,如何让它变成0 ...
- JVM垃圾回收器(三)
垃圾回收知识点 引用计数 给对象添加一个引用计数器,每当一个地方引用这个对象,这个计算器就加1.如果引用失效,那计算器就减1.如果计算器数量为0,那这个对象就是失效的. 但是如果2个对象虽然不用了,但 ...