手把手教学taro-redux
- 一、安装redux
- 二、创建store文件夹
- 三、演示收货地址的状态管理
- 1.创建唯一的store.js
- 2.处理actions中的address.js
- 3.处理actions中的index.js
- 4.处理reducers中的address.js
- 5.处理reducers中的index.js
- 6.在app.js中引入store -- 模块集成完毕
- 7.组件中使用-4步走
一、安装redux
npm install --save redux react-redux redux-thunk
二、创建store文件夹
store.js: 创建全局唯一的store。
actions:用于描述发生什么事件。
reducers:用于action如何改变state树。
三、演示收货地址的状态管理
1.创建唯一的store.js
作用:建立redux和组件间的桥梁
@file(src/store/store.js)import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducers';// window.__REDUX_DEVTOOLS_EXTENSION__ 可使用Redux DevTools插件
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose;// 使用Redux-thunk中间件
const enhancer = composeEnhancers(applyMiddleware(thunk));// 创建store
const store = createStore(reducer, enhancer);export default store;
2.处理actions中的address.js
// 修改收货地址
export const editAddress = (data) => {return { type: 'EditAddress', data }
}
3.处理actions中的index.js
作用:统一导出action
import {editAddress} from './address';export {editAddress,
}
4.处理reducers中的address.js
@file(src/store/reducers/address.js)const addressInfo = {proid: '1',pro: '河南',cityid: '1',city: '洛阳',areaid: '',area: '',
}
const address = (state = addressInfo, action) => {switch (action.type) {case 'EditAddress':return { ...state, ...action.data }default:return state}
}export default address;
5.处理reducers中的index.js
作用:统一导出多个reducer
import { combineReducers } from 'redux';
import address from './address';export default combineReducers({ address,
});
6.在app.js中引入store – 模块集成完毕
此处只写最核心的部分
import { Component } from 'react';
// 1.导入Provider
import { Provider } from 'react-redux'
// 2.导入store
import store from './store/store'
class App extends Component {render() {return (// 3.使用Provider{this.props.children})}
}export default App
7.组件中使用-4步走
// 演示单个redux
import { Component } from 'react'
import { View, Button } from '@tarojs/components'
import Taro from '@tarojs/taro';// 1.引入connect
import { connect } from 'react-redux';
// 2.引入action里的fn
import { editAddress } from '../../store/actions/index';import "taro-ui/dist/style/components/button.scss" // 按需引入
import './address.less';// 3.第三步
@connect(({ address }) => ({ address }), (dispatch) => ({editAddress(data) {dispatch(editAddress(data))},})
)export default class Address extends Component {constructor() {super(...arguments)this.state = {list: [{ pro: '河南', proid: 1, city: '洛阳', cityid: 1, area: '新安', areaid: 1 },{ pro: '河南2', proid: 1, city: '洛阳2', cityid: 1, area: '新安2', areaid: 1 },{ pro: '河南3', proid: 1, city: '洛阳3', cityid: 1, area: '新安3', areaid: 1 },],}}goto = (item) => {// 4.使用action中的fnthis.props.editAddress(item)}render() {return ({/* 展示redux的数据 */}当前地址:{this.props.address.pro + ' ' + this.props.address.city + ' ' + this.props.address.area}修改地址{this.state.list.map(item => {return ({item.pro} {item.city} {item.area})})})}
}
注: connect 方法接受两个参数 mapStateToProps 与 mapDispatchToProps
mapStateToProps,函数类型,接受最新的 state 作为参数,用于将 state 映射到组件的 props
mapDispatchToProps,函数类型,接收 dispatch() 方法并返回期望注入到展示组件的 props 中的回调方法
至此处理完毕,可以在持续集成其他模块的状态管理