在react里面使用redux
2020-07-19 04:23
453 查看
第一步安装 npm i react-redux
第二步 redux数据注入到react应用内
import { Provider } from "react-redux"
Provider组件可以把redux数据注入到react应用里面 这种方法会被{connect}取代,下文会讲到,如下图。
第三步利用connect 把组件和state的数据进行关联 映射为组件的属性(取代Povider)
通过connect之后组件的props中会包含有state中的状态数据和一个dispatch方法
import { connect } from "react-redux"; const mapStateToProps = (state) => state; export default connect(mapStateToProps)(App);
通过combineReducers合并多个reducer为一个
import { createStore, combineReducers, compose, applyMiddleware } from "redux"; import thunk from "redux-thunk"; import counter from "./reducers/counter"; import product from "./reducers/product"; //创建一个reducer仓库 const store = createStore( combineReducers({ counter, product, }), compose( applyMiddleware(...[thunk]), // 同意使用中间件 window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() ) ); export default store;
第四步redux的异步操作
redux里面的action是同步操作,只能做简单操作
vuex的话dispatch派发action为是用来做异步操作的,为mutations改变数据做准备
安装插件 npm i redux-thunk
然后引入
import thunk from "redux-thunk";
compose( applyMiddleware(...[thunk]), // 同意使用中间件 )
相关文章推荐
- react的redux基础使用
- Redux和React-Redux使用关系
- 详解如何优雅地在React项目中使用Redux
- React-redux使用
- redux在react-native上使用(二)--加入redux-saga
- react中redux基础使用笔记
- 使用Redux管理你的React应用(转载)
- 【React】知识点归纳:使用redux来编写一个简单的应用
- redux与react联合使用
- 关于redux与react-redux是一些使用经验
- 在react/redux中使用Immutable
- 使用Redux管理你的React应用
- react-redux异步加载时使用的中间件
- react-redux使用实例
- react学习之redux基本使用(计算器为例)
- 什么是React-redux、为什么使用React-redux、怎么使用React-redux
- React第三方组件5(状态管理之Redux的使用⑤异步操作)
- 学习笔记(01):最新完整react教程从入门到精通包教包会-【必学】redux的使用和模块化封装1...
- redux 配合 react 在项目中的使用(个人总结与备忘)
- React第三方组件5(状态管理之Redux的使用⑥Redux DevTools)