废话不多说,先上一张图

首先记住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. 某里巴巴Java工程师常规面试题以及解答

    从HR弄来的P6-P7的JAVA工程师题目,分享给大家 1 Spring AOP和IOC的实现方法 http://blog.csdn.net/tarena_lixy/article/details/7 ...

  2. Cocos Creator_继承组件单例

    前言 单例,在游戏开发中是比较常用的功能,全局唯一,可以在任何地方直接获取, 省去了方法赋值 或者 属性面板拖动的麻烦. 普通单例_饿汉模式 不管有没调用,一开始就创建单例 1 // Singleto ...

  3. 我对MVC的理解

    1.    MVC :M模型  V视图  C控制器 1.1  模型是用来处理业务逻辑的,里面由许多类构成 1.2  视图是用来显示界面的 1.3  控制器是一个中间人,它通过视图的提交方式(post, ...

  4. Kafka笔记8(管理Kafka)

    使用kafka-topic.sh工具可以执行大部分操作   创建/修改/删除/查看集群里的主题.要使用全部功能,需要通过--zookeeper参数提供zookeerper连接字符串 创建主题: 创建主 ...

  5. navicat 远程访问mariadb失败,修改配置如下

    1.首先配置允许访问的用户,采用授权的方式给用户权限 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '121212' WITH GRAN ...

  6. 感觉不错的随笔 关于C、C++的

    [effective C++的网页版] http://www.kuqin.com/effectivec2e/ 内存四区模型 https://www.cnblogs.com/crazyzhang/p/5 ...

  7. js 闭包,作用域,this 终结篇(转)

    之前有写过闭包,作用域,this方面的文章,但现在想想当时写的真是废话太多了,以至于绕来绕去的,让新手反而更难理解了,所以就有了此篇文章,也好和闭包,作用域,this告一段落. 第一个问题:什么是闭包 ...

  8. oracle之数据恢复(delete误删)

    ALTER TABLE TA_申请材料表 ENABLE row movement ; flashback table TA_申请材料表 to timestamp to_timestamp('2019- ...

  9. centos7 lamp

    yum install libmcrypt libmcrypt-devel mcrypt mhash zlib zlib-devel libpng libpng-devel freetype free ...

  10. rpgmakermv \c 常用颜色一览

    1 2 3 4 5 6 7 14 18