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
相关推荐
化作繁星15 小时前
如何在 React 中测试高阶组件?
前端·javascript·react.js
初遇你时动了情15 小时前
react module.scss 避免全局冲突类似vue中scoped
vue.js·react.js·scss
Au_ust15 小时前
千峰React:函数组件使用(2)
前端·javascript·react.js
来一碗刘肉面16 小时前
React - ajax 配置代理
前端·react.js·ajax
界面开发小八哥18 小时前
可视化工具SciChart如何结合Deepseek快速创建一个React仪表板?
react.js·信息可视化·数据可视化·原生应用·scichart
Java知识技术分享21 小时前
使用LangChain构建第一个ReAct Agent
python·react.js·ai·语言模型·langchain
谢尔登1 天前
【React】React 性能优化
前端·react.js·性能优化
Rowrey1 天前
react+typescript,初始化与项目配置
javascript·react.js·typescript
谢尔登1 天前
Vue 和 React 的异同点
前端·vue.js·react.js
风清云淡_A1 天前
【react18】如何使用useReducer和useContext来实现一个todoList功能
react.js