React状态管理——zustand

安装

复制代码
npm i zustand

基本使用

javascript 复制代码
import React from "react";
import { create } from "zustand";
​
// 1. 创建store
const useStore = create((set) => {
  return {
    // 状态数据
    count: 0,
    inc: (data) => {
      set((state) => ({ count: state.count + data }));
    },
  };
});
​
// 2. 绑定store到组件
​
export default function App() {
  const { count, inc } = useStore();
  return (
    <div>
      <div>count:{count}</div>
      <button onClick={() => inc(1)}>点我+1</button>
    </div>
  );
}

异步支持

javascript 复制代码
import React from "react";
import { create } from "zustand";
​
const URL = "http://geek.itheima.net/v1_0/channels";
// 1. 创建store
const useStore = create((set) => {
  return {
    // 状态数据
    count: 0,
    inc: (data) => {
      set((state) => ({ count: state.count + data }));
    },
    // 异步支持
    // 状态数据
    channelList: [],
    fetchGetList: async () => {
      const res = await fetch(URL);
      const jsonRes = await res.json();
      set(() => ({ channelList: jsonRes.data.channels }));
    },
  };
});
​
// 2. 绑定store到组件
​
export default function App() {
  const { count, inc, fetchGetList, channelList } = useStore();
  return (
    <div>
      <div>count:{count}</div>
      <button onClick={() => inc(1)}>点我+1</button>
      <button onClick={() => fetchGetList()}>异步支持</button>
      {channelList.map((item) => (
        <div>{item.id}</div>
      ))}
    </div>
  );
}

​切片模式

javascript 复制代码
import { create } from "zustand";
​
const URL = "http://geek.itheima.net/v1_0/channels";
// 1. 创建counter相关切片
const counterStore = (set) => {
  return {
    // 状态数据
    count: 0,
    inc: (data) => {
      set((state) => ({ count: state.count + data }));
    },
    asyncInc: (data) => {
      setTimeout(() => {
        set((state) => ({ count: state.count + data }));
      }, 2000);
    },
  };
};
// 2. 创建channel相关切片
const channelStore = (set) => {
  return {
    channelList: [],
    fetchGetList: async () => {
      const res = await fetch(URL);
      const jsonRes = await res.json();
      set(() => ({ channelList: jsonRes.data.channels }));
    },
  };
};
// 3.组合切片
const useStore = create((...a) => {
  return {
    ...counterStore(...a),
    ...channelStore(...a),
  };
});
​
export default function App() {
  const { count, inc, fetchGetList, asyncInc, channelList } = useStore();
  return (
    <div>
      <div>count:{count}</div>
      <button onClick={() => inc(1)}>点我+1</button>
      <button onClick={() => asyncInc(2)}>异步+2</button>
      <button onClick={() => fetchGetList()}>异步支持</button>
      {channelList.map((item) => (
        <div key={item.id}>{item.id}</div>
      ))}
    </div>
  );
}
复制代码
相关推荐
华科云商xiao徐39 分钟前
Java并发编程常见“坑”与填坑指南
javascript·数据库·爬虫
举个栗子dhy1 小时前
解决在父元素上同时使用 onMouseEnter和 onMouseLeave时导致下拉菜单无法正常展开或者提前收起问题
前端·javascript·react.js
前端与小赵1 小时前
vue3和vue2生命周期的区别
前端·javascript·vue.js
一鹿有你们~1 小时前
面试题-前端如何解决跨域
前端·javascript·跨域
Sailing1 小时前
👉 👉 Vue3 自定义 Hook:从入门到进阶(~~安静的阅读2分钟,相信我,这篇文章一定能给你启发)
前端·javascript·vue.js
一枚前端小能手1 小时前
🚀 主线程卡死用户要骂娘?Web Worker让你的应用丝滑如德芙
前端·javascript
小桥风满袖1 小时前
极简三分钟ES6 - Promise
前端·javascript
小高0071 小时前
性能优化零成本:只加3行代码,FCP从1.8s砍到1.2s
前端·javascript·面试
用户66982061129821 小时前
vue3 hooks、utils、data这几个文件夹分别是放什么的?
javascript·vue.js