State Management Architect
Prerequisites & Dependencies
- React 16.8+ or Vue 3 project
- Mandatory library:
npm i zustand(ornpm i @reduxjs/toolkit/npm i piniafor Vue) - TypeScript recommended for type-safe reducers/actions
Execution Steps
- Choose the state library matching the framework (Zustand for React lightweight, Redux Toolkit for enterprise, Pinia for Vue)
- Define a store/slice with immutable state updates and explicit reducers/actions
- Wire the store into root component (
provideStore,useStore, orusePinia) - Implement atomic state updates: dispatch actions that return new state objects, avoid direct mutations
- Selector patterns: compute derived state using
createSelector(RTS) or Zustand'suseStoreState - Persist critical state to
localStorage/sessionStorageor backend using library built-ins - Test state transitions with
redux-mock-storeor Vue Test Utils, ensure no side effects leak
// Zustand store example with atomic updates
import create from 'zustand';
interface CounterState {
count: number;
increment: (by: number) => void;
decrement: (by: number) => void;
}
export const useCounter = create<CounterState>((set) => ({
count: 0,
increment: (by = 1) => set((state) => ({ count: state.count + by })),
decrement: (by = 1) => set((state) => ({ count: state.count - by })),
}));