Redux 进阶之 react-redux 和 redux-thunk 的应用
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 方法中定义两个参数: mapStateToProps和 mapDispatchToProps;
[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);
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 的应用的更多相关文章
- React项目使用Redux
⒈创建React项目 初始化一个React项目(TypeScript环境) ⒉React集成React-Router React项目使用React-Router ⒊React集成Redux Redux ...
- react+redux教程(七)自定义redux中间件
今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...
- React,关于redux的一点小见解
最近项目做多页面应用使用到了,react + webpack + redux + antd去构建多页面的应用,本地开发用express去模拟服务端程序(个人觉得可以换成dva).所以在这里吐槽一下我自 ...
- 在React中使用Redux
这是Webpack+React系列配置过程记录的第六篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...
- react系列(五)在React中使用Redux
上一篇展示了Redux的基本使用,可以看到Redux非常简单易用,不限于React,也可以在Angular.Vue等框架中使用,只要需要Redux的设计思想的地方,就可以使用它. 这篇主要讲解在Rea ...
- React学习之redux
在阅读本文之前,希望大家对以下知识点能提前有所了解并且上好厕所(文章有点长): 状态提升的概念 react高阶组件(函数) es6基础 pure 组件(纯函数) Dumb 组件 React.js的co ...
- react学习之redux和redux-react用法
前言 redux和react-redux的关系: redux就是一个存储数据的对象,并提供了获取/设置store中的属性的解决方案,react-redux是连接react和redux桥梁的封装. ...
- [RN] React Native 使用 Redux 比较详细和深刻的教程
React Native 使用 Redux 比较详细和深刻的教程 React Native 使用 Redux https://www.jianshu.com/p/06fc18cef56a http:/ ...
- React-redux: React.js 和 Redux 架构的结合
通过Redux 架构理解我们了解到 Redux 架构的 store.action.reducers 这些基本概念和工作流程.我们也知道了 Redux 这种架构模式可以和其他的前端库组合使用,而 Rea ...
随机推荐
- OpenCV 学习笔记(13)图像转换成视频
关键 1参数里的分辨率是图像本身的分辨率,而不是指定生成的视频分辨率.如果要修改分辨率,要么后期软件处理,要么读图的时候resize 2要正常退出,不要强制退出. 3生成的只能是avi格式. #inc ...
- EXCEPTION_HIJACK(0xe0434f4e)异常的抛出过程
样例工程 在VS2013里新建一个C#控制台工程,写下如下代码: using System; using System.Collections.Generic; using System.Linq; ...
- shell 字符串拼接
#!/bin/bash name="Shell" url="http://c.biancheng.net/shell/" str1=$name$url #中间不 ...
- Ubuntu 在终端关闭情况下仍然运行进程
参考: 让Linux关闭终端(关闭SSH等)后,程序继续运行 Ubuntu 在终端关闭情况下仍然运行进程 在 Ubuntu 16.04 系统下使用 screen 命令在终端关闭情况下仍然运行进程.具体 ...
- 【转】android实时视频网络传输方案总结(一共有五套)
最近研究了Android的实时视频网络传输问题,在视频处理方面花费了大量精力进行研究,总结出以下五套方案,并加以比较 以320×240大小的视频传输为例 方案 压缩率 压缩/传输方式 实时性 平均流量 ...
- SVN版本管理系统使用教程
1.下载SVN安装包 https://tortoisesvn.net/downloads.html 2.下载SVN汉化包 网页下翻到下载处 3.下载服务端 https://www.visualsvn. ...
- this与bind(this) (es5新增)
this与bind(this) this this指向的是当前函数的作用域(对象实例),有如下的例子 const app = { name: 'xiaoming', log() { console.l ...
- AVLMap平衡二叉树
public class AVLMap<K, V> implements Iterable<AVLEntry<K, V>> { private int size; ...
- 整合去除HTML 标签, 去除开头结尾换行,将连续空格合并为一个
文本是从编辑器中读取数据,有的时候,可能不需要显示格式这个时候需要去除HTML 标签,只引入其中的文本 //去掉html标签 function removeHtmlTab(tab) { return ...
- Qt SDK Issue cstdlib: fatal error: stdlib.h: No such file or directory
*To reproduce the issue, I've tried the following solutions which did not help:* *1) Globally remove ...