react状态管理工具(redux、zustand)

news/2024/7/15 17:14:45 标签: react.js, 前端, javascript, 前端框架

文章目录

  • Redux
    • Redux 中的核心概念
      • Reducer
      • Action
      • Store
      • Redux 的特点
    • 创建store/index.js 作为主文件
    • 创建子文件/store/modules/aadmin.js
    • 导出index.js文件到根index.js使用
    • 使用Redux的方法useSelector,useDispatch两个hooks
  • zustand轻量状态管理
    • Zustand
      • Zustand 的主要特点包括:
      • 解决的问题
    • 安装
    • 创建zustand/index.js文件
    • 使用


Redux

Redux 中的核心概念

Reducer

是一个用来改变 state 的纯函数,传入state 通过应用状态与 Action 推导出新的 state。

Action

Store

  • 存储应用 state 以及用于触发 state 更新的 dispatch 方法等,单一的 Store 。
  • Store 中提供了几个 API :
  • store.getState(): 获取当前 state。
  • store.dispatch(action): 用于 View 发出 Action。
  • store.subscribe(listener): 设置监听函数,一旦 state 变化则执行该函数(若把视图更新函数作为 listener 传入,则可触发视图自动渲染)。

Redux 的特点

  • 单向数据流
  • 单一数据源 ,只有一个 Store 和 Vuex 一样
  • state 是只读的,每次状态更新后返回一个新的 state
  • Store中集成了 dispatch 方法,该方法是 View 发出 Action的唯一途径。

创建store/index.js 作为主文件

import { configureStore } from "@reduxjs/toolkit";

//子模块导入,可自定义多个
import counterStore from "./modules/counterStore";

// 创建store组合子模块
const store = configureStore({
  reducer:{
    counterStore,
  }
})

export default store

创建子文件/store/modules/aadmin.js

这个文件相当于一个对象的数据存储位置

import { createSlice } from "@reduxjs/toolkit";

const counterStore = createSlice({
  name: "todos",
  // 存储数据
  initialState: {
    count:0
  },
  // 方法逻辑函数
  reducers: {
    inscrement(state,action) {
      state.count-=action.payload
    },
    decrement(state,{payload}) {
      state.count+=payload
    },
  },
});

// 导出
export const { decrement, inscrement } = counterStore.actions;
export default counterStore.reducer;

导出index.js文件到根index.js使用

import { Provider } from “react-redux”;
import store from “./store”;
导入并用Provider组件包裹App组件,传入store

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.scss';
import App from './App';
import reportWebVitals from './reportWebVitals';
import store from "./store";
import { Provider } from "react-redux";
import { RouterProvider,router } from "./router";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
      {/* 绑定路由 */}
      <RouterProvider router={router} >
        {/* 绑定redux */}
       <Provider store={store}>
          <App />
       </Provider>
       </RouterProvider>
  </React.StrictMode>
);
reportWebVitals();

使用Redux的方法useSelector,useDispatch两个hooks

useSelector,useDispatch 获取和设置

import { useSelector,useDispatch } from "react-redux";
import { inscrement,decrement } from "./store/modules/counterStore";

import './App.css';

import Home from "./components/Home";
function App() {
  // react-redux中间件导入 useSelector() 函数获取store参数
  const {count} = useSelector(state=>state.counterStore)
  // react-redux中间件导入 useSelector() 函数获取store函数使用
  const dispach = useDispatch()
  return (
    <div 
    className="App active"
    > 
      {count}
      <button type="" onClick={()=>dispach(inscrement(10))}>++</button>
      <button type="" onClick={()=>dispach(decrement(10))}>--</button>
      <Home>
        <span>12222</span>
      </Home>
    </div>
  );
}

export default App;

更多方法请查看Redux官网
到此redux配置完毕

zustand轻量状态管理

Zustand

  • Zustand 是一个轻量级的状态管理库,用于 JavaScript 应用程序,特别是在 React 生态系统中。它提供了一个简单、可扩展的解决方案来中心化和管理应用程序的状态。
  • 与其他状态管理解决方案(如 Redux 或 MobX)相比,Zustand 旨在提供更简洁的 API 和更少的样板代码。它允许你创建一个全局状态存储,并且可以在应用程序的任何地方访问和更新这个状态,而不需要像 Redux 那样编写大量的 action creators 和 reducers。

Zustand 的主要特点包括:

  • 简洁的 API:创建 store 和访问 state 都非常直观。

  • React hooks:Zustand 与 React hooks 完美整合,使得在 React 组件中使用状态变得非常简单。

  • 不可变更新:通过 Immer 库支持,Zustand 允许你以一种直观的方式更新复杂的状态对象。

  • 中间件支持:可以使用中间件来添加额外的功能,例如日志记录、持久化等。

  • 没有“单一真相来源”的限制:与 Redux 不同,Zustand 允许你创建多个独立的 store。

  • Zustand 是在 React 社区内广受欢迎的解决方案之一,适用于那些想要更简单状态管理工具的开发者。

解决的问题

Zustand 主要旨在解决以下问题:

  • 简化状态管理:提供一个更直观的 API,通过避免 Redux 那样的冗余样板代码,使得状态管理更加简洁和直接。

  • 更好的开发体验:通过使用 React Hooks,Zustand 使得在函数组件中访问和更新状态变得容易。

  • 无拘无束:Zustand 允许创建多个独立的 store,不强制要求"单一真相来源",给予开发者更多的灵活性。

  • 性能优化:Zustand 允许组件仅订阅状态的一部分,从而减少不必要的渲染和提高性能。

  • 简单的状态共享:不需要复杂的上下文或提供者,状态可以跨组件和文件轻松共享。

  • 中间件和增强功能:支持中间件,使得开发者可以轻松添加日志记录、持久化存储等增强功能。

  • 适应现代 React 功能:考虑到了 React 的新特性,如 Concurrent Mode 和 Suspense,从而确保在现代 React 特性下的稳定性。

  • 总之,Zustand 应运而生,旨在为开发者提供一个轻便、简单且功能强大的状态管理库,让状态管理变得更加易于使用和维护。

安装

npm i zustand -D

创建zustand/index.js文件

和redux类似,
zustand给每个create函数单独配置,可配置多个,直接导出使用

import { create } from 'zustand'

const useBearStore = create((set) => ({
  bears: 0,
  increasePopulation: () => set((state) => ({ bears: state.bears + 1 })),
  removeAllBears: () => set({ bears: 0 }),
}))

export {
  useBearStore
}

使用

导入useBearStore,
使用useBearStore(state=>state.bears),可直接执行函数,或返回键值对的值

import { useMemo, useState } from "react"
import { useBearStore } from "../zustand/index";
const fib=(n)=>{
  console.log('计算函数执行了');
  if(n<3)
  return 1
return fib(n - 2) + fib( n - 1)
}
function Index() {
  const [count1,setCount1]=useState(0)
  const result =useMemo(()=>{
    fib(count1)
  },[count1])
  const [count2,setCount2]=useState(0)
  const a = useBearStore(state=>state.bears)
  const a1 = useBearStore(state=>state.increasePopulation)
  return (
    <div style={{color:'red'}}>
      Index
      <button type="" onClick={()=>setCount1(count1 +1)}>{count1}</button>
      <button type="" onClick={()=>setCount2(count2 +1)}>{count2}</button>
      <button type="" onClick={a1}>{a}</button>
      {result}
    </div>
  )
}

export default Index;

http://www.niftyadmin.cn/n/5450481.html

相关文章

GPT结合R语言回归模型、多元统计分析、混合效应模型、结构方程实战案例

查看原文>>>科研新边界&#xff1a;GPT & R语言联手&#xff0c;让数据分析不再难&#xff01; 自2022年GPT&#xff08;Generative Pre-trained Transformer&#xff09;大语言模型的发布以来&#xff0c;它以其卓越的自然语言处理能力和广泛的应用潜力&#xf…

启动性能优化

一、应用启动慢的原因 1.在主线程执行了太多耗时的操作&#xff0c;比如加载数据&#xff0c;或者初始化三方库等等&#xff0c;导致在Application的oncreate或者Activity的oncreate方法中耗时太久 2.布局嵌套太深&#xff0c;或者一些不会立即使用的布局也在一开始一起加载到…

maven3.8.1开始不支持http私有库

问题 since maven 3.8.1 http repositories are blocked. 意思是从maven3.8.1版本开始&#xff0c;maven不在认http的私有库&#xff0c;它觉得http私有库不安全。 解决 我直接回退到maven3.8.1之前一个版本&#xff0c;maven3.6.3。我不想去研究settings.xml怎么去配置放心…

从政府工作报告中的IT热词统计探计算机行业发展(三)智能网联新能源汽车:2次

从政府工作报告探计算机行业发展 政府工作报告作为政府工作的全面总结和未来规划&#xff0c;不仅反映了国家整体的发展态势&#xff0c;也为各行各业提供了发展的指引和参考。随着信息技术的快速发展&#xff0c;计算机行业已经成为推动经济社会发展的重要引擎之一。因此&…

ubuntu dpkg执行报错: error processing package indicator-printers (--configure):

在更新ubuntu系统的时候&#xff0c; sudo apt upgrade 碰到报错&#xff1a; Setting up libpaper1:amd64 (1.1.28) ... dpkg: error processing package libpaper1:amd64 (--configure): installed libpaper1:amd64 package post-installation script subprocess returne…

计算机视觉的研究方向

随着科技的快速发展&#xff0c;计算机视觉已成为人工智能领域的一颗璀璨明星。从识别照片中的人物&#xff0c;到自动驾驶汽车的视觉系统&#xff0c;再到医学诊断的辅助工具&#xff0c;计算机视觉正以前所未有的方式改变着我们的生活。在这篇文章中&#xff0c;我们将探讨计…

iOS苹果APP签名更新包与已安装应用的签名不一致的原因及解决方法

大家好我是咕噜美乐蒂&#xff0c;很高兴又和大家见面了&#xff01; iOS苹果APP签名更新包与已安装应用的签名不一致可能出现的原因主要有以下几点&#xff0c;同时给出相应的解决方法&#xff1a; 原因&#xff1a; 1.开发者证书不一致&#xff1a; 更新包使用了与之前应用…

刚刚,百度和苹果宣布联名

百度 Apple 就在刚刚&#xff0c;财联社报道&#xff0c;百度将为苹果今年发布的 iPhone16、Mac 系统和 iOS18 提供 AI 功能。 苹果曾与阿里以及另外一家国产大模型公司进行过洽谈&#xff0c;最后确定由百度提供这项服务&#xff0c;苹果预计采取 API 接口的方式计费。 苹果将…