1. react-redux

  • React-Redux 是 Redux 的官方 React 绑定库。
  • React-Redux 能够使你的React组件从Redux store中读取数据,并且向 store 分发 actions 以更新数据。
  • React-Redux 并不是 Redux 内置,需要单独安装。
  • React-Redux 一般会和 Redux 结合一起使用。

react-redux 安装

$ npm install react-redux

Provider 和 connect

React-Redux 提供<Provider/>组件,能够使你的整个app访问到Redux store中的数据:

App.js:

import React, { Component, Fragment } from 'react';
import { Provider } from 'react-redux';
import store from './store';
import Todolist from './Todolist'; class App extends Component {
render() {
return (
<Provider store={store}>
<Fragment>
<Todolist />
</Fragment>
</Provider>
)
}
} export default App;

React-Redux提供一个connect方法能够让你把组件和store连接起来。

可以在 connect 方法中定义两个参数: mapStateToPropsmapDispatchToProps;

  • [mapStateToProps(state, [ownProps]): stateProps] (Function): 如果定义该参数,组件将会监听 Redux store 的变化。任何时候,只要 Redux store 发生改变,mapStateToProps 函数就会被调用。

  • [mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function): 如果传递的是一个对象,那么每个定义在该对象的函数都将被当作 Redux action creator,对象所定义的方法名将作为属性名;每个方法将返回一个新的函数,函数中dispatch方法会将 action creator 的返回值作为参数执行。这些属性会被合并到组件的 props 中。

例如:在组件中:

import React, { Component } from 'react';
import { connect } from 'react-redux' class TodoList extends Component {
render() {
... ...
}
} // 将store里面的state映射给当前组件,成为组件的props
// 只要 Redux store 发生改变,mapStateToProps 函数就会被调用。该
// 回调函数必须返回一个纯对象,这个对象会与组件的 props 合并
const mapStateToProps = (state) => {
return {
inputValue: state.inputValue,
list: state.list
}
} // 将store.dispatch()方法映射到props上
const mapDispatchToProps = (dispatch) => {
return {
ChangeInputValue(e) {
const action = ...
dispatch(action);
},
handleClick() {
const action = ...
dispatch(action);
}
}
} export default connect(mapStateToProps, mapDispatchToProps)(TodoList);

redux-react API文档

2. redux-thunk

Action 发出以后,Reducer 立即算出 State,这叫做同步;Action 发出以后,过一段时间再执行 Reducer,这就是异步。

  • redux-thunk 是一个常用的 redux 异步 action 中间件。通常用来处理axios请求。
  • redux-thunk 中间件可以让 action 创建函数先不返回一 个action 对象,而是返回一个函数

redux-thunk 用法

$ npm install redux-thunk
import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducer'; const store = createStore(
reducers,
applyMiddleware(thunk)
); export default store;

applyMiddlewares() 是 Redux 的原生方法,作用是将所有中间件组成一个数组,依次执行。

在 actionCreator.js 中:

// 一般,创建 action 的函数只能返回一个对象
export const initListAction = (list) => {
return {
type: constants.INIT_LIST,
list
}
} // --------------------------- // 使用了 redux-thunk, action函数可以返回一个函数
export const getTodoList = () => {
return (dispatch) => {
axios.get('/api/list.json').then(res => {
const { data } = res;
const action = initListAction(data);
dispatch(action);
})
}
}

在 reducer.js 中:

export default (state = defaultState, action) => {

    ··· ···
if (action.type === constants.INIT_LIST) {
let newState = JSON.parse(JSON.stringify(state));
newState.list = action.list;
return newState;
}
return state;
}

在 Todolist.js 中:

componentDidMount () {
const action = getTodoList();
// 这里取到的action是一个函数,当执行dispatch时,会自动执行该函数
store.dispatch(action);
}

3. react-redux 结合 redux-thunk 完整实例(todolist)

https://github.com/caochangkui/todolist-react-redux-thunk

Redux 进阶之 react-redux 和 redux-thunk 的应用的更多相关文章

  1. React项目使用Redux

    ⒈创建React项目 初始化一个React项目(TypeScript环境) ⒉React集成React-Router React项目使用React-Router ⒊React集成Redux Redux ...

  2. react+redux教程(七)自定义redux中间件

    今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...

  3. React,关于redux的一点小见解

    最近项目做多页面应用使用到了,react + webpack + redux + antd去构建多页面的应用,本地开发用express去模拟服务端程序(个人觉得可以换成dva).所以在这里吐槽一下我自 ...

  4. 在React中使用Redux

    这是Webpack+React系列配置过程记录的第六篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...

  5. react系列(五)在React中使用Redux

    上一篇展示了Redux的基本使用,可以看到Redux非常简单易用,不限于React,也可以在Angular.Vue等框架中使用,只要需要Redux的设计思想的地方,就可以使用它. 这篇主要讲解在Rea ...

  6. React学习之redux

    在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...

  7. react学习之redux和redux-react用法

    前言 redux和react-redux的关系:   redux就是一个存储数据的对象,并提供了获取/设置store中的属性的解决方案,react-redux是连接react和redux桥梁的封装. ...

  8. [RN] React Native 使用 Redux 比较详细和深刻的教程

    React Native 使用 Redux 比较详细和深刻的教程 React Native 使用 Redux https://www.jianshu.com/p/06fc18cef56a http:/ ...

  9. React-redux: React.js 和 Redux 架构的结合

    通过Redux 架构理解我们了解到 Redux 架构的 store.action.reducers 这些基本概念和工作流程.我们也知道了 Redux 这种架构模式可以和其他的前端库组合使用,而 Rea ...

随机推荐

  1. opengl第一个工程

    #include <iostream> #include <glad/glad.h> #include <GLFW/glfw3.h> void framebuffe ...

  2. 搜索法 | 1091bfs搜索:三维bfs

    首先我们来理解样例输入: 尺寸:3行4列5片   阈值:2 1 1 1 1 1 1 1 1 1 1 1 1 ------- 0 0 1 1 0 0 1 1 0 0 1 1 ------- 0 1 1 ...

  3. PATB1024科学计数法

    代码是部分正确,只得了13分还有两个测试点没有通过,不知道原因是啥,先不深究了,赶进度. 参考代码: #include<cstdio> #include<cstring> #i ...

  4. Vue实践TS中的一些常见错误解决方案

    mixin报错 import { Component, Prop, Vue ,Mixins} from 'vue-property-decorator' import httpminix from ' ...

  5. 把ubuntu自带的高gcc版本降到低版本(如gcc 3.4)的方法

    转载自: 博客1.博客2 .博客3 步骤 第一步: 下载所需gcc安装包(.deb格式) 手动:老版本gcc下载地址:http://old-releases.ubuntu.com/ubuntu/poo ...

  6. Centos 6.X查看和设置时间时区

    Centos 6.X系列操作系统的修改时区和时间的方法. 一.查看Centos的时区和时间 1.使用date命令查看Centos时区 [root@VM_centos ~]# date -R Mon, ...

  7. 【2019年05月20日】A股滚动市盈率PE历史新低排名

    2010年01月01日 到 2019年05月20日 之间,滚动市盈率历史新低排名. 上市三年以上的公司, 2019年05月20日市盈率在300以下的公司. 1 - 阳光照明(SH600261) - 历 ...

  8. 小心!做 UI 自动化一定要跨过这些坑

    一 .引子 UI自动化,在移动互联网时代的今天,一直都是在各大测试社区最为火爆的一个TOPIC.甚至在测试同行面前一提起自动化,大家就会自然而然的问:“恩,你们是用的什么框架?appium?还是rob ...

  9. 了解编程语言 ----- c# 简介

    1.编程语言 编程语言: 为了实现人与机器的交互,计算机主要识别的就是 0 和 1 语言的发展过程主要分为: 1.面向机器的语言:二进制,汇编 2.面向过程的语言:汇编语言,C语言,B语言, 3.基于 ...

  10. C语言交换两个指针所指位置的数值

    交换指针变量x和y所指向的存储位置处存放的值,不需要第三个位置来存储临时变量.这种方式并没有性能上的优势. void replace(int *x, int *y) { *y = *x ^ *y; * ...