目录
简介
redux 基本思路
redux 基本使用
单独使用 redux
配合 react-redux 使用
redux-devtools
使用修饰器
本文介绍 redux 的使用方法。
主要包括以下几个部分
·redux 是用来管理公共状态,公共 state 都存放在 store 中。使用 createStore()
方法新建一个 store
·直接修改容易引起误操作,需要有条件的操作 store ,不能直接修改
·使用 store.getState()
来获取 state
·使用 store.dispatch(action)
修改 state
·使用 store.subscribe(listener)
实现监听,如果有改动,进行对应的响应操作
·action 是一个对象,基本格式 { type: TEST, payload: { name: 'ddd' } }
·action creator 是 action 生成函数,根据传入的参数生成对应的 action
npm i redux --save
// action type
const COUNT_ADD = "数量增加"
const COUNT_MINUS = "数量减少"
// initial state
const initState = {
test: 'test'
count: 10
}
// reducer
export default function(state = initState, action) {
switch(action.type) {
case COUNT_ADD:
return {
...state,
count: state.count + 1
}
case COUNT_MINUS:
return {
...state,
count: state.count - 1
}
default:
return state
}
}
// action creator
export function countAdd() {
return { type: COUNT_ADD }
}
export function countMinus() {
return { type: COUNT_MINUS }
}
import { combineReducers } from 'redux'
import app from './app'
import user from './user'
export default combineReducers({ app, user })
import { createStore } from 'redux'
import rootReducer from './reducers'
export default createStore(rootReducer)
// 使用中间件
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './reducers'
export default createStore(rootReducer, applyMiddleware(thunk))
const init = store.getState()
console.log(init)
function listener() {
const { count } = store.getState()
console.log(`目前计数为:${count}`)
}
store.subscribe(listener)
// 派发事件
store.dispatch(countAdd())
store.dispatch(countAdd())
store.dispatch(countMinus())
npm i react-redux --save
Provider
,该组件将 store 放到 context 中,方便子孙组件直接使用 storeconnect(mapStateToProps, mapDispatchToProps)
用来将对应的 state 和 dispatch 放到组件的 props 下// ...
import { Provider } from 'react-redux'
import store from './store'
ReactDOM.render(
,
document.getElementById('root')
);
import React from 'react'
import { connect } from 'react-redux'
import { countAdd, countMinus } from '../../store/reducers/app'
import { WingBlank, WhiteSpace,Button } from 'antd-mobile'
class Demo extends React.Component {
render() {
return (
目前计数:{this.props.count}
)
}
}
export default connect(
({ app }) => app,
{ countAdd, countMinus }
)(Demo)
备注:
redux-devtools 是谷歌浏览器的插件,方便调试
需要在代码中打开
// 没有中间件时开启devToolsExtension
import { createStore } from 'redux'
import rootReducer from './reducers'
export default createStore(rootReducer, window.devToolsExtension ? window.devToolsExtension() : f=>f)
// 使用中间件并开启devToolsExtension
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from './reducers'
export default createStore(rootReducer, compose(
applyMiddleware(thunk),
window.devToolsExtension ? window.devToolsExtension() : f=>f
))
修饰器相关知识参考:装饰器模式(Decorator模式)理解、使用
create-react-app 项目为例:
babel-plugin-transform-decorators-legacy
npm i babel-plugin-transform-decorators-legacy --save
{
// ...
"babel": {
// ...
"plugins": [
"transform-decorators-legacy"
// ...
]
},
// ...
}
@babel/plugin-proposal-decorators
npm i @babel/plugin-proposal-decorators --save
{
// ...
"babel": {
// ...
"plugins": [
// ...
["@babel/plugin-proposal-decorators", { "legacy": true }],
]
},
// ...
}
// ...
@connect(
({ test }) => ({ count: test.count}),
{ testCountAdd }
)
class Demo extends React.Component {
// ...
}
export default Demo