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 ...
随机推荐
- 用bitSet做百万级ip去重
如果直接将几百万数据仍到bitset,内存是否够用?实际测试,600万ip放到一个bitSet中,jvm内存会爆. 所以,就简单做了下分组,构建一个HashMap<String, BitSet& ...
- 2-开发共享版APP(搭建指南)-修改包名
https://www.cnblogs.com/yangfengwu/p/11273734.html https://www.cnblogs.com/yangfengwu/p/11273746.htm ...
- 使用docker部署nginx+tomcat架构
架构说明: 使用nginx+tomcat实现动态/静态(资源请求)分离和负载均衡. 参考文档: https://www.runoob.com/docker/docker-tutorial.html 配 ...
- java calendar获取系统当前小时数
calendar获取系统当前小时数 24小时制 Calendar calendar = Calendar.getInstance(); int curHour24 = calendar.get(c ...
- java基础之 hashmap
Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多,但到底是自己学习的总结,就发出来跟大家一起分享,一起讨论. 1.hashma ...
- 分布式系统全局唯一ID生成
一 什么是分布式系统唯一ID 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识. 如在金融.电商.支付.等产品的系统中,数据日渐增长,对数据分库分表后需要有一个唯一ID来标识一条数据或消息, ...
- mongodb compass 启动报错()
报错: 原因:由于直接关闭客户端,进程没关导致下次开启时,后台还是运行的所以无法重新开启 解决办法: 1.查看进程 tasklist | findstr “MongoDBCompass.exe” 2. ...
- [转帖]Druid介绍及入门
Druid介绍及入门 2018-09-19 19:38:36 拿着核武器的程序员 阅读数 22552更多 分类专栏: Druid 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议 ...
- Kafka session.timeout.ms heartbeat.interval.ms参数的区别以及对数据存储的一些思考
Kafka session.timeout.ms heartbeat.interval.ms参数的区别以及对数据存储的一些思考 在计算机世界中经常需要与数据打交道,这也是我们戏称CURD工程师的原因之 ...
- Linux平台上常用到的c语言开发程序
Linux操作系统上大部分应用程序都是基于C语言开发的.小编将简单介绍Linux平台上常用的C语言开发程序. 一.C程序的结构1.函数 必须有一个且只能有一个主函数main(),主函数的名为main. ...