Redux是什么?

  • 是专于状态管理的库
  • 专于状态管理和react解耦
  • 单一状态,单项数据流
  • 核心概念 store state action reducer

Redux工作流

  react 要改变store里的数据,先要开发一个action,action会通过store.dispatch(action)传递给store;

  store会把之前的state和action转发给reducer;

  reducer是一个函数,接收到state和action,做一些处理之后,会返回一个新的state给store;

  store用新的state替换掉之前store里的state;

  store改变之后,react组件会感知store发生改变,这是组件从store里重新取数据,更新组件内容,页面就跟着发生变化了。

  把redux工作流程必做借书过程的话,react组件是“借书的人”,store是“图书管理员”,action 是“借书的人和图书管理员说的话”,reducer是“图书管理员的查书手册”。当借书者(react组件)想要借一本书时,会和图书管理员(store)说一句话(action),这句话有固定格式,必须是对象或者函数,包含type和value,图书管理员(store)听到话后回去查手册(reducer),手册(reducer)会根据之前的数据(state)和这句话(action),告诉图书管理员(store)现在的数据是什么,管理员(store)拿到现在的数据替换老数据。

redux三项原则

  store唯一的

  只有store能改变自己的内容

  reducer必须是纯函数(给定固定的输入,就会有固定的输出,而且不会有副作用)

常用API

  createStore :创建store

const store = createStore(reducer, enhancer);

  store.dispatch :派发action

const action=getInitListAction();
store.dispatch(action);

  store.getState :获取store里所有内容

constructor(props){
super(props);
this.state=store.getState();
}

  store.subscribe :监控store里的state是否改变

constructor(props){
super(props);
this.state=store.getState();
this.listenerStoreChange=this.listenerStoreChange.bind(this);
store.subscribe(this.listenerStoreChange);
}

  文件目录说明

  

actionType.js

export const INPUT_CHANGE = 'InputChange';
export const BTN_CLICK='BtnClick'
export const ITEM_CLICK='ItemClick'
export const INIT_LIST='InitList'
export const GET_INIT_LIST='GetInitList'

actionCreator.js 创建action

import axios from 'axios';
import { INPUT_CHANGE , BTN_CLICK , ITEM_CLICK , INIT_LIST , GET_INIT_LIST} from './actionType';
export const getInputChangAction = (value)=>({
type:INPUT_CHANGE,
value
});
export const getBtnClickAction = ()=>({
type:BTN_CLICK
});
export const getItemClickAction = (index)=>({
type:ITEM_CLICK,
index
});
export const initListAction = (data)=>({
type:INIT_LIST,
data
});
export const getInitListAction = ()=>({
type:GET_INIT_LIST
});

reducer.js

import { INPUT_CHANGE , BTN_CLICK , ITEM_CLICK , INIT_LIST} from './actionType';
//defaultState设置了仓库的默认数据
const defaultState={
inputValue:'123',
list:[1,2]
}
//reducer 返回的必须是一个函数,函数有两个参数,一个是state上一次的数据,另一个是action
//reducer 可以接受state,但是绝不能修改state
export default (state=defaultState,action)=>{
if(action.type===INPUT_CHANGE){
const newState=JSON.parse(JSON.stringify(state));
newState.inputValue=action.value;
return newState;
}
if(action.type===BTN_CLICK){
const newState=JSON.parse(JSON.stringify(state));
newState.list.push(newState.inputValue);
newState.inputValue=''
return newState;
}
if(action.type===ITEM_CLICK){
const newState=JSON.parse(JSON.stringify(state));
newState.list.splice(action.index, 1)
return newState;
}
if(action.type===INIT_LIST){
const newState=JSON.parse(JSON.stringify(state));
newState.list=action.data
return newState;
}
return state;
}

index.js 创建store

import { createStore, applyMiddleware ,compose} from 'redux';
import reducer from './reducer'
import createSagaMiddleware from 'redux-saga'
import mySaga from './sagas'
const sagaMiddleware = createSagaMiddleware();
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose;
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware));
//创建store用redux里的createStore方法
//创建store要把reducer引进来
const store = createStore(reducer, enhancer);
sagaMiddleware.run(mySaga)
export default store;

React-理解Redux的更多相关文章

  1. React 和 Redux理解

    学习React有一段时间了,但对于Redux却不是那么理解.网上看了一些文章,现在把对Redux的理解总结如下 从需求出发,看看使用React需要什么 1. React有props和state pro ...

  2. 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?

    作者:Wang Namelos链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  3. 理解 React,但不理解 Redux,该如何通俗易懂的理解 Redux?(转)

    作者:Wang Namelos 链接:https://www.zhihu.com/question/41312576/answer/90782136来源:知乎 解答这个问题并不困难:唯一的要求是你熟悉 ...

  4. 实例讲解react+react-router+redux

    前言 总括: 本文采用react+redux+react-router+less+es6+webpack,以实现一个简易备忘录(todolist)为例尽可能全面的讲述使用react全家桶实现一个完整应 ...

  5. 如何理解 Redux?

    作者:Wang Namelos 链接:https://www.zhihu.com/question/41312576/answer/90782136 来源:知乎 著作权归作者所有,转载请联系作者获得授 ...

  6. 【前端】react and redux教程学习实践,浅显易懂的实践学习方法。

    前言 前几天,我在博文[前端]一步一步使用webpack+react+scss脚手架重构项目 中搭建了一个react开发环境.然而在实际的开发过程中,或者是在对源码的理解中,感受到react中用的最多 ...

  7. 轻松理解Redux原理及工作流程

    轻松理解Redux原理及工作流程 Redux由Dan Abramov在2015年创建的科技术语.是受2014年Facebook的Flux架构以及函数式编程语言Elm启发.很快,Redux因其简单易学体 ...

  8. 理解Redux以及如何在项目中的使用

    今天我们来聊聊Redux,这篇文章是一个进阶的文章,建议大家先对redux的基础有一定的了解,在这里给大家推荐一下阮一峰老师的文章: http://www.ruanyifeng.com/blog/20 ...

  9. 通俗易懂的理解 Redux(知乎)

    1. React有props和state: props意味着父级分发下来的属性[父组件的state传递给子组件  子组件使用props获取],state意味着组件内部可以自行管理的状态,并且整个Rea ...

  10. 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)

    一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...

随机推荐

  1. Fragment问题集

    最近做一个APP  ,因为在慕课网上学习到了新的方法来做Tab(APP主界面)效果,所以刚学不久久用起来了 用的Fragment实现Tab方法 查询了一下午的安卓资料,关于这个东西是在安卓3.0以后的 ...

  2. python中urllib的整理

    本不想使用这个玩意,奈何看到很多地方使用,随手整理下 urllib模块提供的urlretrieve()函数,urlretrieve()方法直接将远程数据下载到本地 urlretrieve(url, f ...

  3. sklearn datasets模块学习

    sklearn.datasets模块主要提供了一些导入.在线下载及本地生成数据集的方法,可以通过dir或help命令查看,我们会发现主要有三种形式:load_<dataset_name>. ...

  4. Java的学习路线建议(转)

    https://www.cnblogs.com/huaxingtianxia/p/5724093.html

  5. RTX服务端用户数据迁移说明

    步骤一 最好在没有人使用RTX腾讯通的时候,这样你才能保证数据的实时同步;可以在服务器里面把RTX的相关服务器暂停再执行. 步骤二 进入RTX管理器用户数据----导出用户数据---还要把用户照片文件 ...

  6. Paramiko和堡垒机实现

    一.Paramiko paramiko模块,基于SSH用于连接远程服务器并执行相关操作. 1.安装:pip install paramiko 2.SSHClient:用于连接远程服务器并执行基本命令 ...

  7. January 28th, 2018 Week 05th Sunday

    I wish you all I ever wanted for you, I wish you the best. 我希望你不负我的期望,愿你一切安好. I hope I can live up t ...

  8. 超详细!Github团队协作教程(Gitkraken版)

    超详细!Github团队协作教程(Gitkraken版) 一.前期工作 1. 在 Github 上创建 organization step1. 登录Github网站,点击右上角头像,选择 " ...

  9. 写给spring版本的那些事儿

    1.远程调用rmi协议 Exception in thread "main" java.rmi.UnmarshalException: error unmarshalling re ...

  10. Django复习之ORM

    QuerySet数据类型:                        1.可切片,可迭代      [obj,....]                    2.惰性查询:            ...