40_redux_counter应用_redux完善版本
项目结构:

代码:
import React from 'react';
import ReactDOM from 'react-dom';
import store from './redux/store'
import App from './components/app';
function render() {
ReactDOM.render(<App store={store}/>, document.getElementById('root'));
}
//初始化渲染
render()
//订阅监听(store中的状态变化了,就会自动调用重绘)
store.subscribe(render)
index.js
import React, {Component} from 'react'
import * as actions from '../redux/actions'
export default class App extends Component {
increment = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//2.调用store的方法更新状态
this.props.store.dispatch(actions.increment(number))
};
decrement = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//2.调用store的方法更新状态
this.props.store.dispatch(actions.decrement(number))
};
incrementIfOdd = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//2.得到原本的count状态
const count = this.props.store.getState()
//3.判断,满足条件再更新状态
if (count % 2 === 1) {
//调用store方法更新状态
this.props.store.dispatch(actions.increment(number))
}
}
incrementAsync = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//启动延时定时器
setTimeout(() => {
this.props.store.dispatch(actions.decrement(number))
}, 1000)
};
render() {
const count = this.props.store.getState()
// debugger
return (
<div>
<p>click {count} times</p>
<div>
<select ref={select => this.select = select}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button onClick={this.increment}>+</button>
<button onClick={this.decrement}>-</button>
<button onClick={this.incrementIfOdd}>increment odd</button>
<button onClick={this.incrementAsync}>increment async</button>
</div>
</div>
)
}
}
app.jsx
import {createStore} from 'redux';
import {counter} from './reducers';
//生成store对象
const store = createStore(counter);//内部会第一次调用reduer函数得到初始state
console.log(store, store.getState());
export default store
store.js
import {INCREMENT, DECREMENT} from '../redux/action-types';
/*
* 包含所有action creator
* */
//增加
export const increment = (number) => ({
type: INCREMENT, data: number
})
//减少
export const decrement = (number) => ({
type: DECREMENT, data: number
})
actions.js
40_redux_counter应用_redux完善版本的更多相关文章
- 给WPF示例图形加上方便查看大小的格子之完善版本
原文:给WPF示例图形加上方便查看大小的格子之完善版本 去年10月份, 我曾写过一篇"给WPF示例图形加上方便查看大小的格子"的BLOG(http://blog.csdn.net/ ...
- iOS----友盟分享完善版本
分享 详细集成 注意:1.线上集成文档的示例代码对应的是最新版本的SDK,如果你所用的SDK版本类名或者方法名与此文档不符合,请看随包里面的线下文档或者下载使用最新版本的SDK. 设置友盟appkey ...
- 一个自己实现的Vector 完善版本
一个自己实现的Vector(只能处理基本类型数据) 转载自: https://www.ev0l.art/index.php/archives/22/ string 类型不行 bool char* in ...
- 42_redux_counter应用_redux异步版本
前言: redux默认不支持异步编程,需要下载redux插件(异步中间件) 如何下载: npm install --save redux-thunk 项目结构: 代码: import React, { ...
- c coroutine
今天看了下云风c coroutine 代码 博客,发现 coroutine 实现原理其实还比较简单,就用户态栈切换,只需要几十行汇编,特别轻量级. 具体实现 1. 创建一个coroutine: 也就 ...
- Python数据库迁移脚本(终极版)
上次的那几个脚本这次全部整合到了一起,而且后面发现了一个可以使用的ODBC包,于是这次采用的方式就很简单了,直接通过ODBC将InterBase数据库中的数据全部取出来之后通过Python的sqlal ...
- 不一样的角度 解读微信小程序
不一样的角度 解读微信小程序 七月在夏天· 2 天前 前段时间看完了雨果奖中短篇获奖小说<北京折叠>.很有意思的是,张小龙最近也要把应用折叠到微信里,这些应用被他称为:小程序. 含着金钥匙 ...
- 【java 上传+下载】
一.先说说上传 第一步:pom.xml文件 加上 上传文件依赖架包 <dependency> <groupId>commons-fileupload</groupId&g ...
- 文件上传和下载(可批量上传)——Spring(二)
针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...
随机推荐
- php获取数组最后一个值
$array = array(1,2,3,4,5);
- Android Studio中绘制simpleUML类图详细说明及使用
一.Android Studio中安装simpleUML 1.下载simpleUML jar包 地址为:http://plugins.jetbrains.com/ 搜索 simpleUMLCE 2. ...
- python 代理的使用
这里分享一个测试ip的网址 http://ip.filefab.com/index.php scrapy 随机请求头和代理ip的使用原理 import random # 添加一个中间键 cla ...
- ES6系列之let/const及块级作用域
本系列是在平时阅读.学习.实际项目中有关于es6中的新特性.用发的简单总结,目的是记录以备日后温习:本系列预计包含let/const.箭头函数.解构.常用新增方法.Symbol.Set&Map ...
- Python 之 __new__() 方法与实例化
原文链接:https://www.cnblogs.com/ifantastic/p/3175735.html __new__() 是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解, ...
- PVID和VID彻底研究(上) ——PVID的作用及和VID的区别
http://blog.csdn.net/cybertan/article/details/8348752 另外一篇 netgear的官方文档: http://club.netgear.cn/Know ...
- Tomcat中acceptCount,maxConnections、maxThreads的含义及关系
个人对tomcat连接器3个属性maxConnections.maxThreads.acceptCount的理解: 先摘取官网对这3个属性的描述: acceptCount The maximum qu ...
- 基于consul高可用
1.介绍consul Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License ...
- Qt之QTreeWidget入门
QTreeWidget的一些基本操作 1.insertTopLevelItems,insertTopLevelItem用来添加顶层的item QTreeWidget *treeWidget = new ...
- 面试题_lambda函数调用
res多少? def func(): return [lambda x: i * x for i in range(4)] res = [m(2) for m in func()] # print(r ...