项目结构:

代码:

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>&nbsp;
                    <button onClick={this.increment}>+</button>
                    &nbsp;
                    <button onClick={this.decrement}>-</button>
                    &nbsp;
                    <button onClick={this.incrementIfOdd}>increment odd</button>
                    &nbsp;
                    <button onClick={this.incrementAsync}>increment async</button>
                    &nbsp;
                </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完善版本的更多相关文章

  1. 给WPF示例图形加上方便查看大小的格子之完善版本

    原文:给WPF示例图形加上方便查看大小的格子之完善版本 去年10月份, 我曾写过一篇"给WPF示例图形加上方便查看大小的格子"的BLOG(http://blog.csdn.net/ ...

  2. iOS----友盟分享完善版本

    分享 详细集成 注意:1.线上集成文档的示例代码对应的是最新版本的SDK,如果你所用的SDK版本类名或者方法名与此文档不符合,请看随包里面的线下文档或者下载使用最新版本的SDK. 设置友盟appkey ...

  3. 一个自己实现的Vector 完善版本

    一个自己实现的Vector(只能处理基本类型数据) 转载自: https://www.ev0l.art/index.php/archives/22/ string 类型不行 bool char* in ...

  4. 42_redux_counter应用_redux异步版本

    前言: redux默认不支持异步编程,需要下载redux插件(异步中间件) 如何下载: npm install --save redux-thunk 项目结构: 代码: import React, { ...

  5. c coroutine

    今天看了下云风c coroutine  代码 博客,发现 coroutine 实现原理其实还比较简单,就用户态栈切换,只需要几十行汇编,特别轻量级. 具体实现 1. 创建一个coroutine: 也就 ...

  6. Python数据库迁移脚本(终极版)

    上次的那几个脚本这次全部整合到了一起,而且后面发现了一个可以使用的ODBC包,于是这次采用的方式就很简单了,直接通过ODBC将InterBase数据库中的数据全部取出来之后通过Python的sqlal ...

  7. 不一样的角度 解读微信小程序

    不一样的角度 解读微信小程序 七月在夏天· 2 天前 前段时间看完了雨果奖中短篇获奖小说<北京折叠>.很有意思的是,张小龙最近也要把应用折叠到微信里,这些应用被他称为:小程序. 含着金钥匙 ...

  8. 【java 上传+下载】

    一.先说说上传 第一步:pom.xml文件 加上 上传文件依赖架包 <dependency> <groupId>commons-fileupload</groupId&g ...

  9. 文件上传和下载(可批量上传)——Spring(二)

    针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...

随机推荐

  1. Pong 打乒乓

    发售年份 1972 发售平台 多平台 开发商 雅达利(Atari) 类型 体育休闲 https://www.youtube.com/watch?v=fiShX2pTz9A

  2. 网络工具之chisel + openvpn混合

    目的: 访问内网的shared folder 内网可以无缝访问internet而不需要设置代理(因为有些软件没办法支持代理,比如rustup) 解决方案: 基本思路 家里 设置chisel服务开放44 ...

  3. java构造函数和初始化

    1.如果构造函数的第一条语句是一条普通的语句.也就是说,不是对this()或super()的调用,那么java会插入一个隐式的super()调用,从而调用超类的默认构造函数.从该调用返回时,Java将 ...

  4. ThinkPad T460进不去系统的解决方法

    系统卡在系统lenovo界面的尝试解决方案:开机点击F1进入到bios界面进入Security-Secure Boot选Disabled进入Startup-UEFI选BOTH 然后点击F10 选yes ...

  5. GRE配置教程——华为设备

    GRE隧道是通过隧道两端的Tunnel接口建立的,所以需要在隧道两端的设备上分别配置Tunnel接口.对于GRE的Tunnel接口,需要指定其协议类型为GRE.源地址或源接口.目的地址和Tunnel接 ...

  6. promise 基础知识

    promise 基础知识 proise:1.Promise是异步编程的一种解决方案,它有三种状态,分别是pending-进行中.resolved-已完成.rejected-已失败2.创建实例//met ...

  7. Some elementary algorithms on discrete differential geometry(DDGSpring2016 Demos)

    I studied the on-line course(http://brickisland.net/DDGSpring2016/) by myself, and here are the scre ...

  8. CentOS7(64) yum安装、配置PostgreSQL 11

    一.安装postgresql11 1.Install the repository RPM: 添加RPM yum install https://download.postgresql.org/pub ...

  9. C# 6.0:Expression – Bodied Methods

    Expression-bodied 方法是C# 6.0 中另一个能简化代码的特性.我们已经对lambda表达式将funciton和delegation关联起来的这种用法很熟悉了.Expression- ...

  10. multipart/form-data和application/x-www-form-urlencoded区别

    FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型.例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码格 ...