React 第三方状态管理库相关 -- Redux & MobX 篇

一、redux

首先安装依赖:

javascript 复制代码
npm install redux react-redux @reduxjs/toolkit

示例代码:

javascript 复制代码
// src/store/index.js
import { configureStore } from '@reduxjs/toolkit'
import couterSlice from './couterSlice'

const store  = configureStore({
    reducer:{
        couterSlice
    }
})

export default store
javascript 复制代码
// src/store/couterSlice.js
import { createSlice } from '@reduxjs/toolkit';

const couterSlice = createSlice({
    name:'couterSlice',
    initialState:{ count: 0 },
    reducers:{
        increment: (state) => { state.count++ },
        decrement: (state) => { state.count++ }
    },
    extraReducers:(builder) => { }
})
export const { increment,decrement } = couterSlice.actions
export default couterSlice.reducer
javascript 复制代码
// src/main.jsx
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import { Provider } from 'react-redux'
import store from '@/store'

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </StrictMode>,
)

具体使用示例:

javascript 复制代码
// src/App.jsx
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { useSelector, useDispatch } from 'react-redux'
import { increment } from '@/store/couterSlice.js'

function App() {
  const [count, setCount] = useState(0)
  const dispatch = useDispatch()
  const counter = useSelector((state) => state.couterSlice)
  console.log(counter);
  return (
    <>
      <div>
        <a href="https://vite.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://react.dev" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => dispatch(increment())}>
          count is {counter.count}
        </button>
        <p>
          Edit <code>src/App.jsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </>
  )
}

export default App

持久化存储参考: react-redux 数据持久化-CSDN博客

二、MobX

首先安装依赖:

javascript 复制代码
npm install mobx mobx-react-lite

示例代码:

javascript 复制代码
// src/store/index.js
import React from 'react'
import counter from './counterStore'

class RootStore {
    constructor() {
        this.counterStore = counter
    }
}

const rootStore = new RootStore()
const context = React.createContext(rootStore)
const useStore = () => React.useContext(context)
export { useStore }
javascript 复制代码
// src/store/counterStore.js
import { makeAutoObservable } from 'mobx'

class CounterStore {
    count = 0
    constructor() {
        makeAutoObservable(this)
    }
    addCount = () => {
        this.count++
    }
    get Count() {
        return this.count
    }
}

const counter = new CounterStore()
export default counter
javascript 复制代码
// src/App.jsx
import { observer } from 'mobx-react-lite'
import { useStore } from './store'
function App() {
  const store = useStore()
  return (
    <div className="App">
      <button onClick={() => store.counterStore.addCount()}>
        {store.counterStore.count}
      </button>
    </div>
  )
}
export default observer(App)
相关推荐
垃圾侠9 分钟前
vue2版本tinymce简单使用指南
前端·vue.js
LLLuckyGirl~9 分钟前
vite功能之---npm 依赖解析和预构建
前端·npm·node.js
姚永强19 分钟前
登录系统网址作业
开发语言·前端·javascript
努力挣钱的小鑫35 分钟前
【Vue】vue3 video 保存视频进度,每次进入加载上次的视频进度
前端·javascript·音视频
明月看潮生41 分钟前
青少年编程与数学 02-006 前端开发框架VUE 25课题、UI数据
javascript·vue.js·ui·青少年编程·编程与数学
新知图书43 分钟前
Spring MVC复杂数据绑定-绑定集合
前端·spring·mvc
黄团团1 小时前
Vue2+OpenLayers实现车辆开始、暂停、重置行驶轨迹动画(提供Gitee源码)
前端·javascript·数据库·vue.js·gitee·html
Judy16231 小时前
Vue 实现当前页面刷新的几种方法
前端·javascript·vue.js
Southern Wind1 小时前
vue3封装el-tour漫游式引导
前端·typescript·vue
芳草萋萋鹦鹉洲哦1 小时前
【element plus】虚拟dom表格中cellRenderer如何使用v-for循环渲染item
前端·javascript·elementui·虚拟dom表格