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的更多相关文章

  1. Vuex、Flux、Redux、Redux-saga、Dva、MobX

    https://www.jqhtml.com/23003.html 这篇文章试着聊明白这一堆看起来挺复杂的东西.在聊之前,大家要始终记得一句话:一切前端概念,都是纸老虎. 不管是Vue,还是 Reac ...

  2. dva 路由跳转

    1.从props取出并传递history 取 const { history } = this.props 用 <button onClick={ () => history.push(' ...

  3. dva框架之redux相关

    dva封装了redux,减少很多重复代码比如action reducers 常量等,本文简单介绍dva redux操作流程. 利用官网的一个加减操作小实例来操作: dva所有的redux操作是放在mo ...

  4. 前端笔记之React(七)redux-saga&Dva&路由

    一.redux-saga解决异步 redux-thunk 和 redux-saga 使用redux它们是必选的,二选一,它们两个都可以很好的实现一些复杂情况下redux,本质都是为了解决异步actio ...

  5. redux和dva

    实习的时候,公司使用的是react,react说实话生态学的还不是很完善,就暂时先不做跟react相关的博客,等以后学好了react全家桶之后,专门再总结一下react的内容 这两天看了公司的alit ...

  6. UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER

    UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?

  7. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  8. collections 模块(namedtuple, deque, Counter )

    基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型 ...

  9. 【React】启动dva脚手架

    开始前: 确保node版本为6.5以上. // 安装脚手架 npm i dva-cli -g // 自动安装新工程 dva new newProjectName // 导入antd包 npm i an ...

  10. Performance Monitor2:Peformance Counter

    Performance Counter 是量化系统状态或活动的一个数值,Windows Performance Monitor在一定时间间隔内(默认的取样间隔是15s)获取Performance Co ...

随机推荐

  1. StringUtils.isEmpty StringUtils.isBlank

    两个方法都是判断字符是否为空的.前者是要求没有任何字符,即str==null 或 str.length()==0:后者要求是空白字符,即无意义字符.其实isBlank判断的空字符是包括了isEmpty ...

  2. Jupyter Notebook 快捷键使用指南

    因为使用Jupyter Notebook用鼠标选择菜单影响效率,遂将快捷命令记录于此 转自:http://blog.konghy.cn/2017/05/04/jupyter-notebook-hotk ...

  3. 执行多条SQL语句,实现数据库事务(不可传入Sql参数)

    执行多条SQL语句,实现数据库事务(不可传入Sql参数) http://blog.csdn.net/hanxuemin12345/article/details/9980371

  4. Linux /etc/hosts文件

    均为转载 ———————— 1.主机名: 无论在局域网还是INTERNET上,每台主机都有一个IP地址,是为了区分此台主机和彼台主机,也就是说IP地址就是主机的门牌号. 公网:IP地址不方便记忆,所以 ...

  5. CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-1虚拟机安装及环境初始化

    1.软件准备: VMware-workstation-full-14.1.2-8497320.exe CentOS-7-x86_64-DVD-1804.iso 2.VMare激活码: AU5WA-0E ...

  6. web配置详细解释

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  7. css伪类及伪元素用法

    注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的读者可以自己尝试: :active  大致效果为用鼠标 ...

  8. 使用QML绘制界面

    1 使用QML设计登录界面 https://www.cnblogs.com/bhlsheji/p/5324871.html 2 使用QML实现下拉列表框  https://blog.csdn.net/ ...

  9. python基础之Day23

    1.封装 什么是? 封:明确地把属性隐藏起来 ,对外隐藏,对内开放 申请名称空间,往里面装入一系列名字 /属性(类比 类 和对象   只是装的概念) 为什么要用? __init__往对象里丢属性 封装 ...

  10. linux下安装srilm

    1.安装相关依赖库 a.c/c++ compiler:编译器gcc 3.4.3及以上版本   b.GNU make:构建和管理工程的工具,解释Makefile里的指令,描述了整个工程所有文件的编译顺序 ...