dva-counter
dva实例Counter。
import dva, { connect } from 'dva';
import { Router, Route } from 'dva/router';
import styles from './index.less';
const app = dva();
function XX() {
this.events = {};
}
XX.prototype.addListener = function(event, listener) {
if(!this.events.hasOwnProperty(event)) {
this.events[event] = listener;
}
}
XX.prototype.trigger = function(event) {
if(this.events.hasOwnProperty(event)) {
(this.events[event])()
}
}
const xx = new XX();
app.model({
namespace: 'count',
state: {
record: 0,
current: 0,
},
reducers: {
add(state) {
const newCurrent = state.current + 1;
return {
...state,
record: newCurrent > state.record ? newCurrent : state.record,
current: newCurrent
}
},
minus(state) {
return {
...state,
current: state.current - 1
};
},
},
subscriptions: {
clickWatcher({dispatch}) {
xx.addListener('click', () => {dispatch({type: 'minus'})})
}
},
effects: {
*add(action, {call, put}) {
yield call(delay, 1000);
yield put({type: 'minus'});
}
}
});
const SubButton = () => {
return (
<button onClick={() => xx.trigger('click')}>subButton</button>
)
}
const CountApp = ({count, dispatch}) => {
return (
<div className={styles.normal}>
<div className={styles.record}>Highest Record: {count.record}</div>
<div className={styles.current}>{count.current}</div>
<div className={styles.button}>
<button onClick={() => { dispatch({type: 'count/add'}); }}>+</button>
</div>
<SubButton />
</div>
)
}
function mapStateToProps(state) {
return {
count: state.count
};
}
const HomePage = connect(mapStateToProps)(CountApp);
app.router(({history}) =>
<Router history={history}>
<Route path="/" component={HomePage} />
</Router>
)
app.start('#root');
// -------------
// Helpers
function delay(timeout) {
return new Promise(resolve => {
setTimeout(resolve, timeout)
})
}

.normal {
width: 200px;
margin: 100px auto;
padding: 20px;
border: 1px solid #ccc;
box-shadow: 0 0 20px #ccc;
}
.record {
border-bottom: 1px solid #ccc;
padding-bottom: 8px;
color: #ccc;
}
.current {
text-align: center;
font-size: 40px;
padding: 40px 0;
}
.button {
text-align: center;
button {
width: 100px;
height: 40px;
background: #aaa;
color: #fff;
}
}
dva-counter的更多相关文章
- Vuex、Flux、Redux、Redux-saga、Dva、MobX
https://www.jqhtml.com/23003.html 这篇文章试着聊明白这一堆看起来挺复杂的东西.在聊之前,大家要始终记得一句话:一切前端概念,都是纸老虎. 不管是Vue,还是 Reac ...
- dva 路由跳转
1.从props取出并传递history 取 const { history } = this.props 用 <button onClick={ () => history.push(' ...
- dva框架之redux相关
dva封装了redux,减少很多重复代码比如action reducers 常量等,本文简单介绍dva redux操作流程. 利用官网的一个加减操作小实例来操作: dva所有的redux操作是放在mo ...
- 前端笔记之React(七)redux-saga&Dva&路由
一.redux-saga解决异步 redux-thunk 和 redux-saga 使用redux它们是必选的,二选一,它们两个都可以很好的实现一些复杂情况下redux,本质都是为了解决异步actio ...
- redux和dva
实习的时候,公司使用的是react,react说实话生态学的还不是很完善,就暂时先不做跟react相关的博客,等以后学好了react全家桶之后,专门再总结一下react的内容 这两天看了公司的alit ...
- UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER
UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?
- [LeetCode] Design Hit Counter 设计点击计数器
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- collections 模块(namedtuple, deque, Counter )
基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型 ...
- 【React】启动dva脚手架
开始前: 确保node版本为6.5以上. // 安装脚手架 npm i dva-cli -g // 自动安装新工程 dva new newProjectName // 导入antd包 npm i an ...
- Performance Monitor2:Peformance Counter
Performance Counter 是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performance Co ...
随机推荐
- JS高级总结
网址:https://www.cnblogs.com/signheart/p/d6c229a5a758ee1dc21ad5ca2042ab8f.html 通常,通过 JavaScript,您需要操作 ...
- MyEclipse10.0的破解过程详细及图解
MyEclipse10.0的破解过程详细图解 准备阶段 : 1. 破解软件(网上有下载) 2. JDK软件(免费软件) 开始破解: 1. 关闭MyEclipse 10.0 2. 安装 JDK 然后解压 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)
http://codeforces.com/contest/1129/problem/C #include<bits/stdc++.h> #define fi first #define ...
- OGRE Tutorials 1
[Guide to building OGRE] 1.Preparing the build environment You should now create a build directory f ...
- css第四天
四丶 补充:CSS Display(显示) 与 Visibility(可见性) display属性设置一个元素应如何显示,visibility属性指定一个元素应可见还是隐藏. 隐藏元素 - displ ...
- python异常捕捉以及处理
看标题觉得高大上,好像能处理所有的异常.但是,事实是只能按照字面的意思来理解这一段话. 众所周知写代码哪有不出bug的? 那么出现了bug 我们不想让程序因为bug的存在而退出.那么要怎么做呢? 今天 ...
- ES6使用Set实现数组去重
ES6里新添加了两个很好用的东西,Set和Array.from. Set是一种新的数据结构,它可以接收一个数组或者是类数组对象,自动去重其中的重复项目. 常情况下,NaN === NaN 返回的是fa ...
- BootStrap布局组件
BootStrap字体图标(Glyphicons) BootStrap下拉菜单:下拉菜单是可以切换的,是以列表格式显示链接的上下文菜单. 类 描述 .dropdown 指定下拉菜单 .dropdown ...
- 20-matlab全排列-函数调用
matlab中global的用法 Matlab 中子函数不传参直接调用主函数global变量方法 在一个m文件里要调用一个函数(自定义的),但是我希望这个函数能利用并修改workspace中的变量( ...
- statrc部分
statrc部分 1. 如何使用 #- 在app中编写 stark.py #- 在stark.py中进行定制 #- 默认配置: #site.register(models.UserInfo) #- 自 ...