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. thinkphp5 or

    $where['sq']=[ [ 'like' , '%"'.UID.'"%'] , [ 'like' , '%"'.$userinfo['depart_id'].'&q ...

  2. linux卸载erlang

    rpm -qa | grep erlang | xargs rpm -e --nodeps

  3. jmeter入门案例(二)

    jmeter入门简介(一)下载及元件介绍https://www.cnblogs.com/wish5714/p/9714930.html jmeter典型的http请求示例 业务场景 银行卡收单交易,模 ...

  4. 定点CORDIC算法求所有三角函数及向量模的原理分析、硬件实现(FPGA)

    一.CORDIC算法 CORDIC(Coordinate Rotation DIgital Computer)是一种通过迭代对多种数学函数求值的方法,它可以对三角函数.双曲函数和平面旋转问题进行求解. ...

  5. 字节、字、bit、Byte、byte的关系区分

    1.位(bit)             来自英文bit,音译为"比特", 表示二进制位.位是计算机内部数据存储最小单位,11010100是一个8位二进制数.一个二进制位只可以表示 ...

  6. Angularjs 滚动条控制

    控制滚动条 依赖 $location, $anchorScroll $scope.gotoTop = function () { $location.hash("top"); $a ...

  7. Nginx动静分离负载均衡

    前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. Nginx.conf配置 # 定义Nginx运行的用户 和 用户组 如果对应服务器暴露在外 ...

  8. http标码集合

    201 Created告诉客户端,请求成功,资源已经创建.新的资源的网址请看 202 Accepted告诉客户端,请求已经接受,但还没有处理,可以去Location字段查询进展. 200 Ok告诉客户 ...

  9. HashTable和HashMap的区别详解(转)

    一.HashMap简介 HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长. HashMap是非线程安全的, ...

  10. php使用redis的GEO地理信息类型

    redis3.2中增中了对GEO类型的支持,该类型存储经纬度,提供了经纬设置,查询,范围查询,距离查询,经纬度hash等操作. <?php $redis = new Redis(); $redi ...