当前位置: 首页 > news >正文

js:react使用zustand实现状态管理

文档

  • https://www.npmjs.com/package/zustand
  • https://github.com/pmndrs/zustand
  • https://docs.pmnd.rs/zustand/getting-started/introduction

安装

npm install zustand

示例

定义store
store/index.js

import { create } from "zustand";export const useCountStore = create((set) => ({// statecount: 0,// actionincrement: () => set((state) => ({ count: state.count + 1 })),decrement: () => set((state) => ({ count: state.count - 1 })),
}));

使用store

app.jsx

import React, { useState, useEffect } from "react";
import { useCountStore } from "./store/index.js";function FooCounter() {const count = useCountStore((state) => state.count);return <h1>count: {count}</h1>;
}function BarCounter() {const increment = useCountStore((state) => state.increment);const decrement = useCountStore((state) => state.decrement);return (<><button onClick={increment}>increment</button><button onClick={decrement}>decrement</button></>);
}export default function App() {return (<><FooCounter /><BarCounter /></>);
}
http://www.lryc.cn/news/237116.html

相关文章:

  • vue3+vite+SQL.js 读取db3文件数据
  • 微信小程序 限制字数文本域框组件封装
  • 阿里国际站(直通车)
  • C# GC机制
  • wpf devexpress在未束缚模式中生成Tree
  • Python利器:os与chardet读取多编码文件
  • 微服务和注册中心
  • 吴恩达《机器学习》9-1-9-3:反向传播算法、反向传播算法的直观理解
  • Java 算法篇-链表的经典算法:判断回文链表、判断环链表与寻找环入口节点(“龟兔赛跑“算法实现)
  • 【JS】Chapter13-构造函数数据常用函数
  • 06-流媒体-YUV数据在SDL控件显示
  • 对象和数据结构
  • ESP32-BLE基础知识
  • vscode终端npm install报错
  • 雪花算法的使用
  • flink源码分析之功能组件(一)-metrics
  • Nginx反向代理和负载均衡
  • 基于SSM的供电公司安全生产考试系统设计与实现
  • 大数据-之LibrA数据库系统告警处理(ALM-12055 证书文件即将过期)
  • 应试教育导致学生迷信标准答案惯性导致思维僵化-移动机器人
  • 【运维篇】5.4 Redis 并发延迟检测
  • 碰到一个逆天表中表数据渲染
  • 记录我常用的免费API接口
  • 编程的简单实例,编程零基础入门教程,中文编程开发语言工具下载
  • 创芯科技USB_CAN【库文件】
  • React整理总结(四)
  • ajax,axios,fetch
  • Java值传递和引用传递
  • FPGA_IIC代码-正点原子 野火 小梅哥 特权同学对比写法(1)
  • LabVIEW编程开发NI-USRP