State
This project uses zustand as the state management tool, combined with the persistence storage functionality provided by zustand/middleware. Below is an example of counter management, covering state definition, operation methods, and persistence storage configuration.
Example Code
Here's the zustand configuration for counter management, used to manage count state and its operations.
typescript
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
/**
* Counter store type
*/
export interface CountStoreType {
count: number;
increment: () => void;
decrement: () => void;
reset: () => void;
}
export const useCountStore = create<CountStoreType>()(
persist(
(set) => ({
count: 0, // Initial counter value
increment: () => set((state) => ({ count: state.count + 1 })), // Increase count
decrement: () => set((state) => ({ count: state.count - 1 })), // Decrease count
reset: () => set({ count: 0 }), // Reset count
}),
{
// Persistence storage
name: 'countStorage', // Local storage name
}
)
);Description
State Fields
count: Current count value, defaults to0.
Methods
increment: Increase count value, adds1to current value.decrement: Decrease count value, subtracts1from current value.reset: Reset count value to0.
Persistence Storage
Implements state persistence through the persist method from zustand/middleware:
- Storage Name:
countStorage - Storage Method: Uses
localStorageby default, can be customized according to needs.
Usage
Import State Management
In components where you need to use counter state, directly get state and methods through useCountStore. For example:
typescript
import React from 'react';
import { useShallow } from 'zustand/react/shallow';
import { useCountStore } from '@/store/count';
const Counter = () => {
const { count, increment, decrement, reset } = useCountStore(useShallow((state) => ({
count: state.count,
increment: state.increment,
decrement: state.decrement,
reset: state.reset,
})));
return (
<div>
<h1>Current Count: {count}</h1>
<button onClick={increment}>Increase</button>
<button onClick={decrement}>Decrease</button>
<button onClick={reset}>Reset</button>
</div>
);
};
export default Counter;Notes
Persistence Storage Configuration:
- Persistence storage uses
localStorageby default. If you need to use other storage methods (likesessionStorage), you can specifystoragein thepersistconfiguration:typescriptpersist( (set) => ({ // State definition }), { name: 'countStorage', storage: sessionStorage, // Use sessionStorage } );
- Persistence storage uses
State Management Separation:
- Split state management files according to functional modules, for example, put counter management in
count.ts, other modules (like user information, settings, etc.) can create correspondingstorefiles separately.
- Split state management files according to functional modules, for example, put counter management in