
redux使用方式
发布日期:2021-05-08 06:35:21
浏览次数:18
分类:精选文章
本文共 3561 字,大约阅读时间需要 11 分钟。
统一状态管理
安装npm i react-redux --savenpm i react --save
文件的设置
在react框架的src目录下,创建actions和reducers两个文件夹,以及store.js文件配置actions文件夹,
创建actionTypes.js文件export const CHANGE_COUNT = 'CHANGE_COUNT';export const CHANGE_MSGLIST = 'CHANGE_MSGLIST';
创建具体的actions文件,比如chat模块需要用的,创建chat.js
import { CHANGE_COUNT, CHANGE_MSGLIST } from './actionTypes';export const changeCount = (value) => { return { type: CHANGE_COUNT, payload: value, };};export const changeMsgList = (value) => { return { type: CHANGE_MSGLIST, payload: value, };};
配置reducers文件夹
比如,为了chat模块,创建其具体的方法。先创建chat.jsimport { CHANGE_COUNT, CHANGE_MSGLIST } from '../actions/actionTypes';//创建初始化数据,不调用任何dispatch的情况下,返回的数据const INITIAL_STATE = { /* The Color of O2Team Brand */ count: 0, msgList: [],};export default function theme(state = INITIAL_STATE, action) { switch (action.type) { case CHANGE_COUNT: return { ...state, count: action.payload, }; case CHANGE_MSGLIST: return { ...state, msgList: action.payload, }; default: return state; //如果不满足以上所有条件,也就是type的内容都不合法,则返回初始化数据,默认值 }}
创建所有模块的reducer集合
reducers.js文件import { combineReducers } from 'redux'import theme from './theme'import chat from './chat'export default combineReducers({ theme, chat})
配置store.js
import { createStore, applyMiddleware, compose } from 'redux';import thunkMiddleware from 'redux-thunk';//为了异步actions的中间件import rootReducer from '../reducers';//所有的reducer导入//看起来复杂,其实是为了createStore的第二个参数,流程而已const composeEnhancers = typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ : compose;// window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ // // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...// }) : compose//看起来复杂,其实是为了createStore的第二个参数,流程而已const middlewares = [thunkMiddleware];//看起来复杂,其实是为了createStore的第二个参数,流程而已if (process.env.NODE_ENV === 'development' && process.env.TARO_ENV !== 'quickapp') { middlewares.push(require('redux-logger').createLogger());}//看起来复杂,其实是为了createStore的第二个参数,流程而已const enhancer = composeEnhancers( applyMiddleware(...middlewares), // other store enhancers if any);export default createStore(rootReducer,enhancer)
到这里为止,redux的基本配置就结束了。
如何使用redux?
首先在index.js全局中导入store
import { Provider } from 'react-redux';import store from "./store";
render(//在provider组件包裹下,写代码 )
组件中使用步骤
import { connect} from 'react-redux';import { CHANGE_COUNT, CHANGE_MSGLIST } from '../../acitons/actionTypes';export class ReduxTest extends Component { render () { console.log(this.props) return () }}//在这里将store的初始数据,注入到this.propsconst mapStateToProps = (state) => { return { chat: state.chat }}//在这里将actions的方法注入到this.propsconst mapDispatchToProps = (dispatch) => { console.log(dispatch) return { //通过this.props.CHANGE_COUNT 使用 CHANGE_COUNT: () => { dispatch({ ...CHANGE_COUNT, //这是传回去的参数,可以在reducers文件下对应的模块里,对应的type下, //通过action.playload.name获取到,并改变初始化数据,渲染到页面上 playload: { name: "xixi" } }) }, CHANGE_MSGLIST: () => { dispatch({ ...CHANGE_MSGLIST, //这是传回去的参数 playload: { age: 18 } }) } };}//第一个参数是获取所有初始数据的方法,第二个参数是获取所有改变数据的方法,最后的ReduxTest是使用redux子组件的组件名称export default connect(mapStateToProps, mapDispatchToProps)(ReduxTest){ this.props.chat}
发表评论
最新留言
不错!
[***.144.177.141]2025年04月03日 20时55分35秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
秋色园引发CPU百分百命案的事件分析与总结
2021-05-09
安装jdk并配置环境变量
2021-05-09
稀疏数组
2021-05-09
js的严格模式
2021-05-09
ETL工具-KETTLE教程实例实战1----术语和定义
2021-05-09
idea的安装和无限期试用
2021-05-09
Oracle VM VirtualBox安装PVE虚拟机
2021-05-09
【转】如何用css限制文字长度,使溢出的内容用省略号…显示
2021-05-09
Android MediaPlayer setDataSource failed
2021-05-09
ASP.NET Core 实战:Linux 小白的 .NET Core 部署之路
2021-05-09
【nodejs原理&源码杂记(8)】Timer模块与基于二叉堆的定时器
2021-05-09
如何查看jsplumb.js的API文档(YUIdoc的基本使用)
2021-05-09
大前端的自动化工厂(1)——Yeoman
2021-05-09
数据仓库建模方法论
2021-05-09
数据仓库之拉链表
2021-05-09
虚拟机搭建hadoop环境
2021-05-09
DataStax Bulk Loader教程(四)
2021-05-09
物联网、5G世界与大数据管理
2021-05-09
Cassandra与Kubernetes
2021-05-09