废话不多说,先上一张图

首先记住redux设计思想

  Web应用是一个转态机,视图与转态是一一对应的

  所有的转态,保存在一个对象里

1.store

  存储数据的容器,整个应用只有一个state,Redux提供createStore这个函数生成Store,Redux store保存了根reducer返回的完整state树

 const { createStore } = require('redux')
//创建store的时候就要把reducer放进去,reducer改state,state写在reducer里
//store提供三个方法
/*
store.getState() 获取state
store.dispatch(action) 更新state
store.subscribe(listener) 注册监听器
*/
const store = createStore(reducer)
console.log(store)
// subscript是个订阅函数,他在状态发生变化的时候,自动调用
store.subscribe(() => {
console.log(store.getState())
}) // 调用store.dispatch, 会再次执行reducer
setInterval(() => {
store.dispatch({
type: 'increment',
//负载,载荷
payload: 4
})
}, 1000)

2.State

  Store对象包含所有数据。如果想的得到某个时点的数据,就要对Store生成快照。这种时点的数据集合,就叫State。当前时刻的State,可以通过store.getState()拿到。

  一个State对应一个View,只要State相同,View就相同。

3.Action

  View通过Action导致State发生变化

  store.dispatch()是View发出Action的唯一方法。携带一个Action对象作为参数,将它发送出去。

  Action是一个对象。其中的type属性是必须的,表示Action的名称。payload是它携带的信息。

  可以这样理解,Action描述当前发生的事情。改变State的唯一办法,就是使用Action。他会运送数据到Store。

  Action Creator: view要发送多少种消息,就会有多少种Action。通过Action Creator函数来生成Action

4.reducer

  reducer就是实现(state,action)=> newState的纯函数--正真处理state的地方。不修改原来的state,而是通过返回新的state的方式去修改。

  纯函数  同样的输入一定会有同样的输出

const reducer = (state = defaultState, action) => {
// redux会在开始的时候执行一次reducer, action.type = @@redux/INITc.v.p.a.u.f
if (action.type === 'increment') {
return {
//state = 1 直接修改state不允许 state + 1 可以
//state定义对象,在这里返回的时候也要返回对象
count: state.count + action.payload
}
}
return state
}

撸到这里是不是有点云里雾里,

下面来看一个redux实现的加减栗子

Counter.js

import React, { Component } from 'react'

import store from './store'

class Counter extends Component {
constructor (props) {
super(props)
this.increment = this.increment.bind(this)
this.decrement = this.decrement.bind(this) this.state = {
count: store.getState()
} store.subscribe(this.changeCount.bind(this))
}
render () {
return (
<div>
<button onClick={this.decrement}>-</button>
{ this.state.count }
<button onClick={this.increment}>+</button>
</div>
)
}
increment () {
store.dispatch({
type: 'increment'
})
}
decrement () {
store.dispatch({
type: 'decrement'
})
} changeCount () {
this.setState({
count: store.getState()
})
}
} export default Counter

  store.js

import { createStore } from 'redux'

const defaultState = 1

const reducer = (state = defaultState, action) => {
switch (action.type) {
case 'increment' :
return state + 1
case 'decrement' :
return state - 1
default :
return state
}
} const store = createStore(reducer) export default store

初识redux走向redux-react的更多相关文章

  1. Redux:with React(一)

    作者数次强调,redux和React没有关系(明明当初就是为了管理react的state才弄出来的吧),它可以和其他插件如 Angular, Ember, jQuery一起使用.好啦好啦知道啦.Red ...

  2. [转] What is the point of redux when using react?

    As I am sure you have heard a bunch of times, by now, React is the V in MVC. I think you can think o ...

  3. [Redux] Filtering Redux State with React Router Params

    We will learn how adding React Router shifts the balance of responsibilities, and how the components ...

  4. [Redux] Navigating with React Router <Link>

    We will learn how to change the address bar using a component from React Router. In Root.js: We need ...

  5. 25.redux回顾,redux中的action函数异步

    回顾:Redux: 类似于 Vuex 概念:store/reducer/action action:动作 {type,.....} 一定要有type 其他属性不做限制 reducer:通过计算产生st ...

  6. React-安装和配置redux调试工具Redux DevTools

    chrome扩展程序里搜索Redux DevTools进行安装 新建store的时候,进行如下配置. import { createStore, applyMiddleware ,compose} f ...

  7. Redux 和 Redux thunk 理解

    1: state 就像 model { todos: [{ text: 'Eat food', completed: true }, { text: 'Exercise', completed: fa ...

  8. [Redux] Understand Redux Higher Order Reducers

    Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...

  9. 手把手教你撸一套Redux(Redux源码解读)

    Redux 版本:3.7.2 Redux 是 JavaScript 状态容器,提供可预测化的状态管理. 说白了Redux就是一个数据存储工具,所以数据基础模型有get方法,set方法以及数据改变后通知 ...

随机推荐

  1. iOS 控制器的生命周期(UIController)

    前言: 在iOS开发中,控制器的生命周期非常重要,什么时候加载页面,什么时候请求接口,什么时候刷新界面等等,都有很多值得优化的地方 loadView: 最先执行的方法,控制器关联的有Nib文件的时候, ...

  2. pandas处理时间序列(4): 移动窗口函数

    六.移动窗口函数 移动窗口和指数加权函数类别如↓: rolling_mean 移动窗口的均值 pandas.rolling_mean(arg, window, min_periods=None, fr ...

  3. Ubuntu server LTS 16.04安装SSH以及连接问题

    1.SSH安装 出现问题: 登录到Ubuntu服务器,执行以下命令: sudo apt-get install openssh-server 出现以下错误: 解决办法: 1)确保服务器能出外网,比如说 ...

  4. VMVare的窗口自适应

    啊!好久没来博客园了.原因很简单,我把密码丢了. 最近才从系统申请重置了密码,这不,又能登录了.你可能好奇,是的,我也在疑惑:我是不是搞IT的啊?因为只要密码丢失,我就认为世界完蛋了,我完蛋了:) 这 ...

  5. NuGet的简单使用

    什么是NuGet? NuGet(读作New Get)是用于微软.NET开发平台的软件包管理器,是一个Visual Studio的扩展.在使用Visual Studio开发基于.NET Framewor ...

  6. tree状数据叶子节点与根节点等的递归转换

    做项目时经常遇到树状层级数据.从各个层级数据的转换查询等.场景如行业类别的多层级,行政区层级,检查项类别层级等等. 数据结构如  Id Name ParentId #region area树状节点的转 ...

  7. EXCEL对比在职员工与离职员工

    EXCEL 在B1中 填写这个 =VLOOKUP(A1,C:C,1,0)    然后往下拉  只要有出现#N/A  说明已经离职了 公司需要

  8. 在sql中case子句的两种形式

    case子句,在select后面可以进行逻辑判断. 两种形式:判断相等.判断不等 一.判断相等的语法: case 列名 when ...  then ... when ...  then ... el ...

  9. linux 查看CPU、内存、磁盘信息命令

    [1]查看CPU信息 (1)查看CPU信息(型号) cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c (2)查看物理CPU个数 cat /pr ...

  10. dos模式下切换电脑用户

    启用管理员运行dos 然后输入net user adminstrator /active.yes 然后点击打开按钮 就可以切换电脑用户了