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>
  );
}
复制代码
相关推荐
脑袋大大的3 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络4 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码
耶啵奶膘4 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
JoJo_Way4 小时前
LeetCode三数之和-js题解
javascript·算法·leetcode
视频砖家5 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
小白变怪兽7 小时前
一、react18+项目初始化(vite)
前端·react.js
GISer_Jing8 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆8 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding9 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js