zustand 切片模式使用,persist 中间件持久化状态

zustand - npm

安装 npm i zustand

创建切片目录:

创建切片 channelStore.js

复制代码
import { getChannelsAPI } from "@/apis/article";
const channelStore = (set) => {
    return {
        channelList: [],
        fetchChannelList: async () => {
            const res = await getChannelsAPI()
            set({
                channelList: res.data.data.channels
            })
        }
    }
}
export default channelStore

创建切片numStore.js

复制代码
const numStore = (set) => {
    return {
        num: 0,
        incre: ()=>{
            set(state => ({
                num: state.num+1
            }))
        }
    }
}
export default numStore
创建主Store 如 useStoree 并组合切片
复制代码
import { create } from "zustand";
import channelStore from "./module/channelStore";
import numStore from "./module/numStore";

const useStoree = create((...args) =>{
    return {
        ...channelStore(...args),
        ...numStore(...args)
    }
})
export default useStoree

或者

复制代码
import { create } from 'zustand';
import channelStore  from './module/channelStore';
import numStore  from './module/numStore';

const useStoree = create(set => ({
  ...channelStore(set),
  ...numStore(set)
}))

export default useStoree

在组件中使用useStoree

复制代码
import useStoree from "@/zustandStore";
import { useEffect } from "react";

const ZustanInd = ()=>{
    // const num = useStoree((state) => state.num)
    // const fetchChannelList = useStoree((state) => state.fetchChannelList)
    // const channelList = useStoree((state) =>state.channelList)
    // const incre = useStoree((state) => state.incre)

    const {num,incre,fetchChannelList,channelList} = useStoree()
    
    useEffect(()=>{
        fetchChannelList()
    },[])
    console.log(channelList)

    return<>
    <button onClick={incre}>{num}</button>
    <ul>
        {channelList.map(item=><li key={item.id}>{item.name}</li>)}
    </ul>
    </>
}
export default ZustanInd

使用 persist 中间件 持久化状态

npm install zustand/persist

复制代码
import { create } from 'zustand';
import channelStore  from './module/channelStore';
import numStore  from './module/numStore';
import { persist } from 'zustand/middleware';
const useStoree = create(
    persist(
        set => ({
            ...channelStore(set),
            ...numStore(set)
          }),
          {
            name: 'my-store', // 存储的名字
            getStorage: () => localStorage, // 存储的方式
          }
    )
)

export default useStoree

持久化部分:

复制代码
import { create } from 'zustand';
import channelStore  from './module/channelStore';
import numStore  from './module/numStore';
import { persist } from 'zustand/middleware';
const useStoree = create(
    persist(
        (set) => ({
            ...channelStore(set),
            ...numStore(set)
          }),
          {
            name: 'my-store', // 存储的名字
            getStorage: () => localStorage, // 存储的方式 ,默认是 localStorage,也可设置 sessionStorage
            partialize: (state) => ({ num: state.num }), // 持久化部分状态
          }
    )
)

export default useStoree
相关推荐
早點睡3901 小时前
基础入门 React Native 鸿蒙跨平台开发:react-native-flash-message 消息提示三方库适配
react native·react.js·harmonyos
早點睡3902 小时前
高级进阶 ReactNative for Harmony项目鸿蒙化三方库集成实战:react-native-image-picker(打开手机相册)
react native·react.js·harmonyos
早點睡3902 小时前
基础入门 React Native 鸿蒙跨平台开发:react-native-easy-toast三方库适配
react native·react.js·harmonyos
●VON12 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
光影少年12 小时前
react状态管理都有哪些及优缺点和应用场景
前端·react.js·前端框架
冻感糕人~14 小时前
【珍藏必备】ReAct框架实战指南:从零开始构建AI智能体,让大模型学会思考与行动
java·前端·人工智能·react.js·大模型·就业·大模型学习
lbb 小魔仙17 小时前
【HarmonyOS实战】React Native 鸿蒙版实战:Calendar 日历组件完全指南
react native·react.js·harmonyos
LYFlied19 小时前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
AAA阿giao1 天前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
摘星编程2 天前
React Native鸿蒙版:Image图片占位符
react native·react.js·harmonyos