React状态管理工具zustand

ops/2024/10/21 9:26:31/

zustand 是极简的状态管理工具

zustand快速上手

创建store(状态数据,操作方法) = = =(绑定组件)= = =》 component(消费数据和方法)

  1. 安装:npm i zustand
  2. 创建store
  3. 绑定store到组件
import {create} from 'zustand'
// 1. 创建store
const useStore = create((set) => {return {// 状态数据count: 0,// 修改状态数据的方法inc: () => {// set是用来修改数据的专门方法,必须调用它来修改数据// 语法1:参数是函数,需要用到老数据的的场景set((state) => ({count: state.count + 1}))// 语法2:参数直接是一个对象// set({count: 100})}}
})function App() {// 2.绑定store到组件const {count, inc} = useStore()return (<div><button onClick={inc}>{count}</button></div>);
}export default App;

zustand异步支持

对于异步的支持不需要特殊的操作,直接在函数中编写异步逻辑,最后只需要调用set方法传入新状态即可

import {create} from 'zustand'
import {useEffect} from "react";const useStore = create((set) => {return {channelList: [],fetchChannelList: async () => {const res = await fetch("http://localhost:3333/channels")const jsonRes = await res.json()console.log(jsonRes);set({channelList: jsonRes.data})}}
})function App() {const {fetchChannelList, channelList} = useStore()useEffect(() => {fetchChannelList()}, [fetchChannelList])return (<div><ul>{channelList.map(item => <li key={item.id}>{item.name}</li>)}</ul></div>);
}export default App;

zustand切片模式

场景:当单个store比较大的时候,可以使用切片模式进行模块拆分组合,类似于模块化

import {create} from 'zustand'
import {useEffect} from "react";// 1.拆分子模块
const createCounterStore = (set) => {return{count: 0,inc: () => {set((state) => ({count: state.count + 1}))},}
}const createChannelStore = (set) => {return {channelList: [],fetchChannelList: async () => {const res = await fetch("http://localhost:3333/channels")const jsonRes = await res.json()console.log(jsonRes);set({channelList: jsonRes.data})}}
}
// 2.组合模块
const useStore = create((...a) => {return {...createCounterStore(...a),...createChannelStore(...a)}
})function App() {// 3.使用const {count, inc,fetchChannelList, channelList} = useStore()useEffect(() => {fetchChannelList()}, [fetchChannelList])return (<div><button onClick={inc}>{count}</button><ul>{channelList.map(item => <li key={item.id}>{item.name}</li>)}</ul></div>);
}export default App;

http://www.ppmy.cn/ops/127230.html

相关文章

Spring Boot 接口数据加解密

今天聊下接口安全问题&#xff0c;涉及到接口的加密和解密 经常和外部单位接口调用梳理了相关技术方案&#xff0c;主要的需求点如下&#xff1a; 1&#xff0c;尽量少改动&#xff0c;不影响之前的业务逻辑 2&#xff0c;考虑到时间紧迫性&#xff0c;可采用对称性加密方式&…

Axure零基础深入浅出的讲解

在当今的互联网产品设计领域&#xff0c;原型设计已经成为了产品经理、设计师和开发者之间沟通的桥梁。而Axure作为一款功能强大、灵活易用的原型设计工具&#xff0c;正是很多产品经理的得力助手。无论你是产品经理新手&#xff0c;还是资深设计师&#xff0c;Axure都能帮助你…

青少年编程能力等级测评CPA C++(三级)-试卷2

青少年编程能力等级测评CPA C&#xff08;三级&#xff09;-试卷2 一、单项选择题&#xff08;共15题&#xff0c;每题3分&#xff0c;共45分&#xff09; CP3_2_1&#xff0e;在宽度为500米的河道上&#xff0c;修建一个拦河大坝。施工队每天筑坝50米&#xff0c;由于当时条件…

【力扣 | SQL题 | 每日4题】力扣1164,3293,1308,1270

4 mid&#xff0c;四题都比较简单&#xff0c;没什么难度。 1. 力扣1164&#xff1a;指定日期的产品价格 1.1 题目&#xff1a; 产品数据表: Products ------------------------ | Column Name | Type | ------------------------ | product_id | int | | new_p…

神奇的数据结构 —— 跳表

《神奇的数据结构——跳表》 在计算机科学的广阔领域中&#xff0c;有一种独特而高效的数据结构——跳表。 跳表&#xff0c;全称为跳跃表&#xff0c;是一种可以替代平衡树的数据结构。它的诞生为解决特定的数据存储和检索问题提供了一种新颖而有效的方法。 从结构上看&…

Matlab自学笔记三十九:日期时间型数据的算术运算:加减运算

1.说明 时间点和&#xff08;日历&#xff09;持续时间是可加的&#xff0c;结果是时间点&#xff1b;两个时间点是可减的&#xff0c;结果是持续时间&#xff0c;用时分秒表示&#xff1b;时间型和浮点数运算&#xff0c;结果是时间型&#xff0c;浮点数默认单位是天&#xf…

51单片机应用——直流电机PWM调速

目标实现功能 单片机引脚输出PWM波形控制直流电机以不同转速工作。 1.PWM简介 PWM技术 PWM的中文全称是脉宽调制&#xff0c;常用于电动机控制、开关电源、音频放大器等。利用PWM技术可以达到微处理器&#xff08;如单片机&#xff09;的数字输出对模拟电路控制的效果。 P…