42_redux_counter应用_redux异步版本
前言:
redux默认不支持异步编程,需要下载redux插件(异步中间件)
如何下载:
npm install --save redux-thunk
项目结构:

代码:
import React, {Component} from 'react'
import PropTypes from 'prop-types'
export default class Counter extends Component {
static propTypes = {
count: PropTypes.number.isRequired,
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
incrementAsync: PropTypes.func.isRequired
}
increment = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//2.调用store的方法更新状态
this.props.increment(number)
};
decrement = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//2.调用store的方法更新状态
this.props.decrement(number)
};
incrementIfOdd = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
//2.得到原本的count状态
const count = this.props.count
//3.判断,满足条件再更新状态
if (count % 2 === 1) {
//调用store方法更新状态
this.props.increment(number)
}
}
incrementAsync = () => {
//1.得到选择的增加数量
const number = this.select.value * 1
this.props.incrementAsync(number)
};
render() {
const {count} = this.props
// 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>
)
}
}
counter.jsx
import React from 'react'
import {connect} from "react-redux";
import {decrement, increment, incrementAsync} from "../redux/actions";
import Counter from '../components/counter'
export default connect(
state => ({count: state}),
{increment, decrement, incrementAsync}
)(Counter)
app.jsx
/* * 包含所有action type的常量字符串 * */ export const INCREMENT = 'INCREMENT'; export const DECREMENT = 'DECREMENT';
action-types.js
import {INCREMENT, DECREMENT} from '../redux/action-types';
/*
* 包含所有action creator
* 同步的action都是返回一个对象
* 异步的action返回的是一个函数
* */
//增加
export const increment = (number) => ({
type: INCREMENT, data: number
})
//减少
export const decrement = (number) => ({
type: DECREMENT, data: number
})
//异步action
export const incrementAsync = (number) => {
return dispatch => {
//异步的代码
setTimeout(() => {
//1S之后才去分发一个增加的action
dispatch(increment(number))
}, 1000)
}
}
actions.js
/*
* 包含n个reducer函数的模块
* */
export function counter(state = 0, action) {
console.log('counter()', state, action)
switch (action.type) {
case 'INCREMENT':
return state + action.data
case 'DECREMENT':
return state - action.data
default:
return state
}
}
reducers.jsx
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk'
import {counter} from './reducers';
//生成store对象
const store = createStore(
counter,
applyMiddleware(thunk)//应用上异步中间件
);//内部会第一次调用reduer函数得到初始state
console.log(store, store.getState());
export default store
store.js
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'
import App from './containers/app';
import store from './redux/store'
ReactDOM.render(
<Provider store={store}>
<App/>
</Provider>, document.getElementById('root')
);
index.js
42_redux_counter应用_redux异步版本的更多相关文章
- struts 多文件上传 annotation注解(零配置)+ ajaxfileupload + 异步 版本
[本文简介] struts 多文件上传.基于”零配置“+"ajaxfileupload" 的一个简单例子. [导入依赖jar包] jquery-1.7.2.js : http:// ...
- 40_redux_counter应用_redux完善版本
项目结构: 代码: import React from 'react'; import ReactDOM from 'react-dom'; import store from './redux/st ...
- java8的版本对组合式异步编程
讨论了Java 8中的函数式数据处理,它可以将对集合数据的多个操作以流水线的方式组合在一起.本节继续讨论Java 8的新功能,主要是一个新的类CompletableFuture,它是对65节到83节介 ...
- [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程
怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html ...
- [Android]异步 layout inflation(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5829809.html 异步 layout inflation ...
- Code First系列之视图,存储过程和异步API
返回<8天掌握EF的Code First开发>总目录 本篇目录 视图View 存储过程 使用存储过程CRUD 异步API 本章小结 自我测试 本系列的源码本人已托管于coding上:点击查 ...
- 异步编程系列第04章 编写Async方法
p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...
- 异步编程系列06章 以Task为基础的异步模式(TAP)
p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...
- Python-09-线程、进程、协程、异步IO
0. 什么是线程(thread)? 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆 ...
随机推荐
- 【申嵌视频】基于VMWare虚拟机下安装ubuntu操作系统的详细步骤
[申嵌视频]基于VMWare虚拟机下安装ubuntu操作系统 适合搭建mini2440, Tiny6410, smart210,Tiny4412, NanoPC-T2, NanoPC-T3, Nano ...
- E4A 与JS交互事件无反应
今天碰到一个问题,E4A与JS的交互,调用JS函数后,事件没有任何反应,给JS赋值,会看到浏览框中有内容显示,但是事件为什么就没反应呢. 把官方的例程打开编译试了也不行. 后来在群中问了,原来是这里设 ...
- GCD学习
出自简书 : https://www.jianshu.com/p/2d57c72016c6 在 GCD 中有两种队列:串行队列和并发队列.两者都符合 FIFO(先进先出)的原则.两者的主要区别是:执行 ...
- 必须学会git和maven
转自: http://tieba.baidu.com/p/3458400116 很多人应该用过svn cvs之类的代码版本管理工具,git也是其中之一. svn和git最大的几个区别要点,svn必须要 ...
- kafka reset offset 手工重置offset
1.场景 a)有时消费端逻辑修改,需要重复消费数据,需要将offset设置到指定位置. 2.实现 kafka版本:0.11.* KIP-122: Add Reset Consumer Group Of ...
- Caching in Presto
转自:Caching in Presto Qubole’s Presto-as-a-Service is primarily targeted at Data Analysts who are tas ...
- 设计模式之Factory Method模式
作用:将实例的生成交给子类 用Template Method模式来构建生成实例的工厂,这就是Factory Method模式. 在Factory Method中,父类决定实例的生成方式,但并不决定所要 ...
- python 代理的使用
这里分享一个测试ip的网址 http://ip.filefab.com/index.php scrapy 随机请求头和代理ip的使用原理 import random # 添加一个中间键 cla ...
- Oracle中rownum的基本用法
Oracle中rownum的基本用法 对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且r ...
- 生成器函数_yield_yield from_send
生成器函数 ''' yield类似于return 相同: 都返回出去一个值 不同: yield每次返回时, 会记录当前执行的位置 等下次调用生成器, 会从该位置向下走 return 直接终止函数 '' ...