此项目模板是使用Create React App构建的,它提供了一种简单的方法来启动React项目而无需构建配置。

使用Create-React-App构建的项目包括对ES6语法的支持,以及几种非官方/尚未最终形式的Javascript语法,

先看效果



分析代码为

1.入口文件

import React from 'react'
import ReactDOM from 'react-dom'
//引入redux
import { createStore } from 'redux'
//入口引入组件
import Counter from './components/Counter'
//引入reducer
import counter from './reducers' const store = createStore(counter)
const rootEl = document.getElementById('root')
// 通过dispatch一个action来set state
const render = () => ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
/>,
rootEl
) render()
//订阅subscribe
store.subscribe(render)

reducer纯函数

//reducer.js
//纯函数state改变
export default (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
//counter组件
import React, { Component } from 'react'
import PropTypes from 'prop-types' class Counter extends Component {
constructor(props) {
super(props);
this.incrementAsync = this.incrementAsync.bind(this);
this.incrementIfOdd = this.incrementIfOdd.bind(this);
} incrementIfOdd() {
if (this.props.value % 2 !== 0) {
this.props.onIncrement()
}
} incrementAsync() {
setTimeout(this.props.onIncrement, 1000)
} render() {
const { value, onIncrement, onDecrement } = this.props
return (
<p>
Clicked: {value} times
{' '}
<button onClick={onIncrement}>
+
</button>
{' '}
<button onClick={onDecrement}>
-
</button>
{' '}
<button onClick={this.incrementIfOdd}>
Increment if odd
</button>
{' '}
<button onClick={this.incrementAsync}>
Increment async
</button>
</p>
)
}
} Counter.propTypes = {
value: PropTypes.number.isRequired,
onIncrement: PropTypes.func.isRequired,
onDecrement: PropTypes.func.isRequired
} export default Counter

Redux Counter example的更多相关文章

  1. Redux Counter Vanilla example

    此示例不需要构建系统或视图框架,并且存在以显示与ES5一起使用的原始Redux API. 代码如下 <!DOCTYPE html> <html> <head> &l ...

  2. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

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

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

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

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

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

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

  6. redux+flux(一:入门篇)

    React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Faceb ...

  7. 通过Redux源码学习基础概念一:简单例子入门

    最近公司有个项目使用react+redux来做前端部分的实现,正好有机会学习一下redux,也和小伙伴们分享一下学习的经验. 首先声明一下,这篇文章讲的是Redux的基本概念和实现,不包括react- ...

  8. redux介绍与入门

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Helvetica } p.p2 { margin: 0.0px 0.0px 0.0px 0. ...

  9. Redux教程3:添加倒计时

    前面的教程里面,我们搭建了一个简单红绿灯示例,通过在console输出当面的倒计时时间:由于界面上不能显示倒计时,用户体验并不良好,本节我们就添加一个简单的倒计时改善一下. 作为本系列的最后一篇文章, ...

随机推荐

  1. springboot 学习之路 8 (整合websocket(1))

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  2. Linux命令学习总结:ls

    命令简介: ls命令用来列出目标目录(缺省的话为当前目录)中所有的子目录和文件.指令英文原义:list 指令所在路径:/bin/ls 执行权限:All User 命令语法: ls [OPTION].. ...

  3. Python运算符之翩若惊鸿,婉若游龙

    python中的运算符算术运算符:主要用于两个对象算数计算(加减乘除等运算)比较运算符:用于两个对象比较(判断是否相等.大于等运算)赋值运算符:用于对象的赋值,将运算符右边的值(或计算结果)赋给运算符 ...

  4. c/c++ 重载运算符 函数调用运算符

    重载运算符 函数调用运算符 把一个类的对象a,当成函数来使用,比如a(),所以需要重载operator()方法.重载了函数调用运算符的类的对象,就是函数对象了. 还有什么是函数对象呢??? lambd ...

  5. mas录屏,带系统声音和麦克风声音

    自带的QuickTime + Soundflower 可完美解决,同时录系统的声音和mic声音,也可以只录系统声音. 安装Soundflower 在应用程序 -> 实用工具,里面找到“音频 MI ...

  6. SQLServer删除数据

    使用SSMS删除数据 1.连接数据库.选择数据表->右键点击,选择所有行(或者选择前200行). 2.在数据窗口中选择数据行(注意点击最左边列选择整个数据行)->在最左侧右键点击-> ...

  7. 基于udp简单聊天的系统

    老师博客:http://www.cnblogs.com/Eva-J/articles/8244551.html#_label4 基于udp的简单的聊天代码 说明:这段代码,显示有client向serv ...

  8. Kafka 0.11.0.0 实现 producer的Exactly-once 语义(中文)

    很高兴地告诉大家,具备新的里程碑意义的功能的Kafka 0.11.x版本(对应 Confluent Platform 3.3)已经release,该版本引入了exactly-once语义,本文阐述的内 ...

  9. VMware Workstation中安装linux系统(CentOS)超详细(部分转载)

    首先准备一下VMware虚拟机和linux镜像文件,链接如下: 对于32位windows机子安装的是10.0.7版本的VMware Workstation,链接: https://pan.baidu. ...

  10. Linux:Day13(上) CentOS系统启动流程

    CentOS 5和6的启动流程 Linux:kernel+rootfs kernel:进程管理.内存管理.网络管理.驱动程序.文件系统.安全功能 rootfs: glibc 库:函数集合,functi ...