agentleFS
bkjain655bkjain655/agent-skills/skills/zustand-persist-state/SKILL.md

zustand-persist-state

Skill0 starsChanged 4 months agoMITView on GitHub

Client state with Zustand: store design, persist to localStorage, rehydration guards, schema migrations, and selectors. Use when adding client-side state, wizard data persistence, or local-first apps in React/Next.js.

  • Installs packages

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Keep actions on the store.

    line 30

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Zustand Persist State

Install

npm install zustand

Store shape

interface AppStore {
  data: AppData;
  currentStep: Step;
  isComplete: boolean;
  setData: (patch: Partial<AppData>) => void;
  setCurrentStep: (step: Step) => void;
  resetAll: () => void;
}

Keep actions on the store; components call actions, not set directly from UI.

Persist middleware

import { create } from "zustand";
import { persist } from "zustand/middleware";

export const useStore = create<AppStore>()(
  persist(
    (set, get) => ({
      ...initialState,
      setData: (patch) => set((s) => ({ data: { ...s.data, ...patch } })),
      resetAll: () => set(initialState),
    }),
    {
      name: "app-storage-key",
      partialize: (s) => ({ data: s.data, currentStep: s.currentStep, isComplete: s.isComplete }),
      merge: (persisted, current) => ({
        ...current,
        ...(persisted as Partial<AppStore>),
        data: normalizeData((persisted as AppStore)?.data ?? current.data),
        currentStep: migrateStep((persisted as AppStore)?.currentStep),
      }),
    }
  )
);

Hydration hook

let hydrated = false;
export const useStoreHydrated = () => {
  const [ready, setReady] = useState(hydrated);
  useEffect(() => {
    const unsub = useStore.persist.onFinishHydration(() => {
      hydrated = true;
      setReady(true);
    });
    useStore.persist.rehydrate();
    return unsub;
  }, []);
  return ready;
};

Gate routes/UI until ready to avoid flash/wrong redirects.

Migrations

When schema changes:

function normalizeData(raw: unknown): AppData {
  // coerce missing fields, rename keys, drop deprecated
  return { ...defaults, ...(raw as AppData) };
}

function migrateStep(step: unknown): Step {
  if (typeof step === "string" && VALID_STEPS.includes(step as Step)) return step as Step;
  return "intro";
}

Version field optional: persistedState.version → switch migration path.

Selectors

export const useProfile = () => useStore((s) => s.data.profile);
export const useWizardComplete = () => useStore((s) => s.isComplete);

Import / export (local-first backup)

export function downloadJsonSnapshot(state: PersistedSnapshot, filename: string) {
  const blob = new Blob([JSON.stringify(state, null, 2)], { type: "application/json" });
  // trigger download
}

Validate imported JSON with Zod before merging into store.

Privacy note

localStorage is plain text — document in privacy policy; offer export + clear.

Anti-patterns

  • Storing formatted display strings instead of raw numbers/dates
  • Whole-store subscription causing global re-renders
  • Skipping merge/normalize → broken state after deploy
  • Reading localStorage synchronously on first client render before rehydrate

Pair with: multi-step-wizard-flow, react-hooks-patterns.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.