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),寄存器集合和堆 ...
随机推荐
- Ansible 批量修改密码
客户要求每3个月修改一次主机密码.密码规则为客服提供的一串字符 xxxx + 主机后3位. 将需要登录主机添加到 Ansible. 将需要登录主机的公钥添加到 known_hosts ssh-keys ...
- 关于怎样获取DevExpress GridView过滤后或排序后的数据集问题(转)
GridView用自带的过滤功能过滤数据后,想要获取过滤后的数据集,有两种方式: 一.笨办法就是循环遍历GridView,根据gridView.GetRow()或者gridView.GetDataRo ...
- 2019年3月更新 技术分享 WPF基本界面制作
1.制作流程1.在vs中建立一个wpf程序2.建立一个主页面(.cs)(注:C#程序每一个页面都由两个文件构成一个xaml一个cs,一个前端文件一个后台文件)3.在主页面中添加按钮,按钮中嵌入图片,这 ...
- swoole之代码热更新实现 转自https://blog.csdn.net/nep_tune/article/details/81329918
随着swoole的版本迭代更新,已经足够稳定了,在阿里,腾讯,yy等各大公司都有着使用,也有很多游戏圈里的朋友也在使用,这些朋友经常会提到一个问题,每次代码更新还需要停止服务,然后重新启动,来达到更新 ...
- 程序员装X指南
一.准备工作“工欲善其事必先利其器.” 1.电脑不一定要配置高,但是双屏是必须的,越大越好,能一个横屏一个竖屏更好.一个用来查资料,一个用来写代码 .总之要显得信息量很大,效率很高. 2.椅子不一 ...
- 时光如梭,MES生产制造执行系统上线2周年--->2016.08,发个博客展示一下系统的主要功能!
以下程序是系统当中的主要功能信息,一些相对简单功能就不在此处展示了. 1.模具基础资料Excel导入与模具资料手动更新功能.友情提示:为了避免不必要的麻烦已经将部分信息打码.! 2.配方资料Excel ...
- Python高级变量类型
1.列表基础操作 2.列表循环遍历 3.元组 4.字典基础操作 5.字典循环遍历 6.字符串基础操作 7.字符串切片 8.公共方法
- linux中开启snmp协议
1.安装软件#yum -y install net-snmp 2.修改配置文件#vim /etc/snmp/snmpd.conf确保snmpd.conf文件中包含以下绿色内容:com2sec notC ...
- Retrofit 下载网络图片 保存到本地
private void downImage(String imagePath) { try { CommonV2Api.downloadFile(mContext, imagePath, new I ...
- 知识点:Mysql 索引原理完全手册(1)
知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) Mysql-索引原理完全手 ...