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. vue-lazyload 图片依赖加载

    一. vue lazyload插件: 插件地址:https://github.com/hilongjw/vue-lazyload   (点击里面的demo进入可以查看使用代码  https://git ...

  2. ubuntu单用户修改密码

    系统版本:Ubuntu 16.04,其它版本类似. 第一步 重启 Ubuntu ,并长按shift键,进入grub菜单,上下键选择Ubuntu高级选项 第二步 上下键选择recovery mode,不 ...

  3. linux重装rabbitmq的问题

    一.卸载 [root@zabbix_server lib]# rpm -qa|grep rabbitmq rabbitmq-server--.noarch [root@zabbix_server li ...

  4. nginx开机启动

    centos 7以上是用Systemd进行系统初始化的 Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建n ...

  5. 拦截过滤防御XSS攻击 -- Struts2.3 以及 2.5 的解决方式

    使用Struts2框架开发的后台在防御XSS攻击的时候很多方式都不能用,因为Struts2对请求进行的二次封装有区别.以下针对Struts2的XSS攻击进行拦截过滤防御解决: Struts2.3 本方 ...

  6. 一.javascript核心部分:1.词法结构

    本文作为个人学习笔记,一直也没有重视javascript的系统学习(javascript是最容易被人忽视的语言),我都是要用的时候百度一下查找下资料开始用,但没有系统的,学习,和整理过javascri ...

  7. SourInsight4 配置视野内引用高亮

  8. C#少量数据分页方法

    string sql = @"select [Name],[Value],[TypeCode] from [dbo].[SYS_Parameter] WHERE TypeCode = 'Us ...

  9. poj2828(线段树查找序列第k小的值)

    题目链接:https://vjudge.net/problem/POJ-2828 题意:有n个人,依次给出这n个人进入队列时前面有多少人p[i],和它的权值v[i],求最终队列的权值序列. 思路:基本 ...

  10. Appium+python自动化获取toast消息的方法

    转载地址:https://www.cnblogs.com/shangren/p/8191879.html 1. 首先执行这个命令:npm install -g cnpm --registry=http ...