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 ...
随机推荐
- Anaconda3(4)安装pytorch
安装链接 https://pytorch.org/ 0在anaconda3安装python3.6环境 https://blog.csdn.net/u012005313/article/details ...
- dos下编辑过sh脚本后无法运行
情景: 同样的的脚本,在不同的机器下运行有的可以成功,有的运行失败, 前提:sh脚本windos下编辑过. 脚本在windows上修改之后,在每行尾部多了个 ^M,导致sh脚本在执行的时候可能shba ...
- Layui 关闭当前标签页
parent.layui.admin.events.closeThisTabs();
- 【转】30种MySQL索引优化的方法
第一方面:30种mysql优化sql语句查询的方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by涉及的列上建立索引. 2.应尽量避免在 where ...
- map和object互转的几种方式
1.使用Apache中的BeanUtils类,导入commons-beanutils包. 2.使用JSON 3.利用java.lang.reflect.Field类完成Obj和Map之间的相互转换 h ...
- Problem B. 即时战略 ———2019.10.12
题目: 代码~:感谢土蛋 #include <iostream> #include <cstring> #include <cmath> #include &l ...
- VQA视觉问答基础知识
本文记录简单了解VQA的过程,目的是以此学习图像和文本的特征预处理.嵌入以及如何设计分类loss等等. 参考资料: https://zhuanlan.zhihu.com/p/40704719 http ...
- Configuration类的@Value属性值为null
今天写的Configuration类的@Value属性值为null @Configuration public class MybatisConfigurer { @Value("${spr ...
- 出现Strict Standards: Only variables should be passed by reference in的解决方法
出现Strict Standards: Only variables should be passed by reference in的解决方法 代码报错: <br /><b> ...
- LRU缓存简单实现
缓存接口定义 /** * 缓存接口 * * @author zhi * */ public interface ICache<K, V> { /** * 添加缓存数据 * * @param ...