store:

import {createStore,applyMiddleware, compose} from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers/rootReducer"; const middlewares = [
applyMiddleware(thunk)
];
const store = initialState => createStore(rootReducer, initialState, compose(...middlewares)); export default store;

rootReducer:

import {combineReducers} from "redux";
import {reducer as toastr} from "react-redux-toastr"; const rootReducer = combineReducers({
toastr,
counterReducer
}); export default rootReducer;

counterReducer:

import * as types from "../actions/actionType";

const initialState = {
count: 0
}; const counterReducer = function (state = initialState, action) {
const count = state.product;
switch (action.type) {
case types.INCREMENT:
return {count: count + 1};
case types.DECREMENT:
return {count: count - 1};
default:
return state; } }; export default counterReducer;

actionType:

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';

Counter:

import {connect} from "react-redux";
import * as types from "actions/actionType"; const mapStatetoProps = state =>{
return {
value: state.counterReducer.count
}
}
const mapDispatchToProps =dispatch => {
return {
onIncrement: ()=>dispatch({type: types.INCREMENT}),
onDecrement: ()=>dispatch({type: types.DECREMENT})
}
} class Counter extends React.Component {
render() {
const {count, onIncrement, onDecrement} = this.props;
return (
<div>
<span>{count}</span>
<button onClick={onIncrement}>+</button>
<button onClick={onDecrement}>-</button>
</div>
)
}
}
const CounterContainer = connect(
mapStateToProps,
mapDispatchToProps
)(Counter)
export default CounterContainer;

counterAction:

import * as types from "./actionType";
export function increment(count) {
return {
type: types.INCREMENT,
count
}
}
export function decrement(count) {
return {
type: types.DECREMENT,
count
}
}
onIncrement: ()=>dispatch({type: types.INCREMENT}) =》
onIncrement: (count)=>dispatch(actions.increment(count)),

use redux-actions:

import {createAction} from "redux-actions";
import * as types from "./actionType";
export const increment = createAction(types.INCREMENT);
export const decrement = createAction(types.DECREMENT);

Redux Thunk 中间 件 可以 让 action 创建 函数 先不 返回 action 对象, 而是 返回 一个 函数。 通过 这个 函数 延迟 dispatch 或者 只在 满足 指定 条件 的 情况下 dispatch。 这个 内部 函数 接受 store 的 两个 方法 dispatch 和 getState 作为 参数。

update action:

import {createAction} from "redux-actions";

import * as types from "../../constant/ActionType";

export  const increment = createAction(types.INCREMENT);
export const decrement = createAction(types.DECREMENT); export function incrementIfOdd() {
return (dispatch, getStore) => {
const count = getStore().CounterReducer.count;
if (count % 2 ==0) {
return
}
dispatch(increment())
}
}

update container :

import React from "react";
import {connect} from "react-redux";
import {Row, Col, Button} from "antd"; import * as actions from "../../reduxModel/actions/CounterAction";
import "./index.scss"; const mapStateToProps = state => {
return {value: state.CounterReducer.count}
};
const mapDispatchToProps = {
onIncrement: actions.increment,
onDecrement: actions.decrement,
onIncrementIfOdd: actions.incrementIfOdd
}; class Home extends React.Component{ render() {
const {value, onIncrement, onDecrement, onIncrementIfOdd} = this.props;
return (
<div className="app-home">
<div className="app-layout-container">
<Row type="flex" justify="center" className="app-layout-body">
<Col span={4}>
sidebar
</Col>
<Col span={20} className="page-panel">
welcome Home
<div className="form-btn-group">
<span>{value}</span>
<Button type="dashed" htmlType="button" onClick={onIncrement}>+</Button>
<Button type="dashed" htmlType="button" onClick={onDecrement}>-</Button>
<Button type="dashed" htmlType="button" onClick={onIncrementIfOdd}>add if odd</Button>
</div>
</Col>
</Row>
</div>
</div>
)
}
}
const HomeContainer = connect(
mapStateToProps,
mapDispatchToProps
)(Home); export default HomeContainer;

react-redux: counter的更多相关文章

  1. react+redux教程(六)redux服务端渲染流程

    今天,我们要讲解的是react+redux服务端渲染.个人认为,react击败angular的真正“杀手锏”就是服务端渲染.我们为什么要实现服务端渲染,主要是为了SEO. 例子 例子仍然是官方的计数器 ...

  2. react+redux教程(四)undo、devtools、router

    上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...

  3. react+redux教程(一)connect、applyMiddleware、thunk、webpackHotMiddleware

    今天,我们通过解读官方示例代码(counter)的方式来学习react+redux. 例子 这个例子是官方的例子,计数器程序.前两个按钮是加减,第三个是如果当前数字是奇数则加一,第四个按钮是异步加一( ...

  4. React+Redux学习笔记:React+Redux简易开发步骤

    前言 React+Redux 分为两部分: UI组件:即React组件,也叫用户自定义UI组件,用于渲染DOM 容器组件:即Redux逻辑,处理数据和业务逻辑,支持所有Redux API,参考之前的文 ...

  5. react案例->新闻移动客户端--(react+redux+es6+webpack+es6的spa应用)

    今天分享一个react应用,应在第一篇作品中说要做一个react+redux+xxx的应用.已经做完一部分,拿出来分享.github地址为:点我就可以咯~ 这里实现了一个新闻移动站的spa.本来想写p ...

  6. React Redux 与胖虎

    这是一篇详尽的 React Redux 扫盲文. 对 React Redux 已经比较熟悉的同学可以直接看 <React Redux 与胖虎他妈>. 是什么 React Redux 是 R ...

  7. webpack+react+redux+es6开发模式

    一.预备知识 node, npm, react, redux, es6, webpack 二.学习资源 ECMAScript 6入门 React和Redux的连接react-redux Redux 入 ...

  8. react+redux教程(五)异步、单一state树结构、componentWillReceiveProps

    今天,我们要讲解的是异步.单一state树结构.componentWillReceiveProps这三个知识点. 例子 这个例子是官方的例子,主要是从Reddit中请求新闻列表来显示,可以切换reac ...

  9. react+redux官方实例TODO从最简单的入门(6)-- 完结

    通过实现了增-->删-->改-->查,对react结合redux的机制差不多已经了解,那么把剩下的功能一起完成吧 全选 1.声明状态,这个是全选状态 2.action约定 3.red ...

  10. react+redux官方实例TODO从最简单的入门(1)-- 前言

    刚进公司的时候,一点react不会,有一个需求要改,重构页面!!!完全懵逼,一点不知道怎么办!然后就去官方文档,花了一周时间,就纯react实现了页面重构,总体来说,react还是比较简单的,由于当初 ...

随机推荐

  1. CAS单点登录实践(spring cas client配置)

    前言: 最近的项目需要将多个站点统一登录,查阅了资料Jasig cas(Central Authentication Service)(官方站点:http://www.jasig.org/cas)使用 ...

  2. GSM/GPRS/3G/4G

    1.状态机机制的gprs拨号 像GPRS/3G模块之类的应用,需要连接,登陆,初始化等步骤完成后才能传输数据,而这些步骤又比较耗时. 所以用 状态机 + 超时 的机制来实现比较合理. 如下代码片段来描 ...

  3. web项目的getContextPath()

    伯乐一看小编的这个博文的标题是不是觉得有些小,以点到面,知道了web中getContextPath()这种获取路径的方式,显然其他的方式的是可以以此类推的.常说,工作学习找共同点嘛. 上一段我们也提高 ...

  4. HadoopHA简述

    1 概述 在hadoop2.0之前,namenode只有一个,存在单点问题(虽然hadoop1.0有 secondarynamenode,checkpointnode,buckcupnode这些,但是 ...

  5. Bootstrap实现的页面

    实现的效果如图,使用bootstrap需要至少三个文件 去bootstrap网上下载,然后使用这三个文件可以了 使用方式,通过标签,class命名来引用已经定制好的html样式 <!DOCTYP ...

  6. Delphi 正则表达式语法(2): 或者与重复

    Delphi 正则表达式语法(2): 或者与重复 // | 号的使用, | 是或者的意思 var   reg: TPerlRegEx; begin   reg := TPerlRegEx.Create ...

  7. GIT学习笔记(5):变基

    GIT学习笔记(5):变基rebase 变基 引入变基 在Git中整合来自不同分支的修改主要有两种方法:merge以及rebase. 整合分支最容易的方法是merge,他会把两个分支的最新快照以及两者 ...

  8. CodeForces - 919D Substring (拓扑排序+dp)

    题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...

  9. JSP SERVLET 基础知识

    jsp(java server page)和servlet是JAVA EE规范的两个基本成员,是JAVA WEB开发的重点也是基础知识.JSP本质上也需要编译成SERVLET运行. JSP比较简单,可 ...

  10. 当root用户无密码,非超级权限用户时提示mysqladmin: Can't turn off logging; error: 'Access denied; you need the SUPER privilege for this op解决方案

    问题: 在centOS上安装了mysql后,卸载了又重新安装,使用mysqladmin -u root password 'new password' 更改密码,提示: mysqladmin: Can ...