笔记:React 状态管理

Redux

React-Redux

Redux-Toolkit

Redux-Thunk

Redux-Saga

MobX v6

Dva

Redux
React-Redux
npm install -g redux react-redux
Action
同步Action

  • 同步Action返回一个对象。
  • actionType一般用一个常量文件保存,便于管理。
    export const ACTIONTYPE = ‘actionType’
    export const action = args => ({ type: ACTIONTYPE, args })
    异步Action
  • redux-thunk用于创建异步Action,返回一个函数。
  • 异步Action中会调用同步Action,并且会传入dispatch()方法。
    export const asyncAction = args => () => {
    setTimeout(dispatch => {
    dispatch(action(args))
    }, 1000)
    }
    Reducer
    // 初始化仓库数据
    const initState = {}
    export default function reducer (preState=initState, action) {
    const { type, args } = action
    switch (type) {
    case ACTIONTYPE:
    // 不要使用数组的push、unshift方法
    return newData
    default:
    return preState
    }
    }
    Store
  • 创建包含异步Action的store。
    import { createStore, applyMiddleware, combineReducers } from ‘redux’
    import thunk from ‘redux-thunk’
    import reducer1 from ‘./’
    import reducer2 from ‘./’
    const allReducer = combineReducers({
    store1: reducer1,
    store2: reducer2,
    })
    export default createStore(allReducer, {}, applyMiddleware(thunk))
  • 提供下所有需要state的组件。
    import { Provider } from ‘react-redux’
    import store from ‘./’

const root = ReactDOM.createRoot(document.getElementById(‘root’));
root.render(
<React.StrictMode>



</React.StrictMode>
);
React UI组件获取状态数据

  • React UI组件需要包裹一层容器组件,容器组件与Redux进行数据通信,容器组件再将数据状态传给React UI组件通过props 获得数据。
  • connect()监听state数据变化并渲染组件
    import { connect } from ‘react-redux’
    import action from ‘./’
    // 容器组件给UI组件传state
    const mapStateToProps = state => ({ key: state.store2.value })
    }
    // 容器组件给UI组件传action
    const mapDispatchToProps = dispatch => ({
    fun: args => dispatch(action(args))
    })
    // 返回一个容器组件
    export default const connect(mapStateToProps, mapDispatchToProps)(A)
    Hooks
    Redux-Toolkit
    npm install -g @reduxjs/toolkit
    Redux-Saga
  • Redux-Saga是一个Redux的中间件,基于Generator实现
    npm install -g redux-saga
    Saga关联Redux仓库
    import { createStore, applyMiddleware } from ‘redux’
    import reducer from ‘./’
    import saga from ‘./’
    import createSagaMiddleware from ‘redux-saga’
    const sagaMiddleware = createSagaMiddleware()
    export default createStore(reducer, {}, applyMiddleware(sagaMiddleware))
    sagaMiddleware.run(saga)
    自定义Saga监听Action
    import { takeEvery, takeLatest, throttle } from ‘reudx-saga/effects’

export function * saga() {
yield takeEvery(‘actionType’, function * () {})
yield takeLatest(‘actionType’, function * () {})
yield throttle(100, ‘actionType’, function * () {})
}
MobX v6
npm install -g mobx mobx-react-lite
创建模块化仓库
import { makeObservable } from ‘mobx’

export default class aStore {
constructor() {
makeObservable(this)
}
// state
state = ‘数据’
// action
action = () => {}
// computed
get computed () {}
}
创建总仓库
import aStore from ‘./’
import bStore from ‘./’
import React from ‘react’

class RootStore {
constructor () {
this.aStore = new aStore()
this.bStore = new bStore()
}
}
const rootStore = new RootStore()
const context = React.createContext(rootStore)
const useStore = () => React.useContext(context)
export { useStore }
组件从仓库中获取数据
import { useStore } from ‘./’
import { observer } from ‘mobx-react-lite’

function A () {
const { aStore } = useStore()
return (
<div onClick={ aStore.action }>
{ aStore.state } + { aStore.computed }

)
}
export default observer(A)
autoRun
reaction
runInAction

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我才是真的李成贤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值