Updated now: Share Zustand State via URL (Copyable Links)
Updated for Zustand v5: persist state in the URL hash so users can copy, share, and restore React app state across sessions and devices.
Updated for Zustand v5: persist state in the URL hash so users can copy, share, and restore React app state across sessions and devices.
Updated now (Jan 28, 2026): refreshed for Zustand v5 syntax and the current URL-hash storage recommendation.
This stores a Zustand counter in the URL hash. Copy the link, reload it, or open it in another tab, and the store comes back in the same state.
Let's start by setting up the initial code for our example application. We have a simple counter state using Zustand:
# Zustand v5 (latest as of Jan 28, 2026)
pnpm add zustand@^5.0.10import { create } from "zustand";
const useCounter = create()((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
}));To achieve this, we need to add the HashStorage utilities:
import { create } from "zustand";
import { createJSONStorage, persist, StateStorage } from "zustand/middleware";
export const hashStorage: StateStorage = {
getItem: (key): string => {
const searchParams = new URLSearchParams(location.hash.slice(1));
const value = searchParams.get(key) ?? "";
return JSON.parse(value);
},
setItem: (key, newValue): void => {
const searchParams = new URLSearchParams(location.hash.slice(1));
searchParams.set(key, JSON.stringify(newValue));
location.hash = searchParams.toString();
},
removeItem: (key): void => {
const searchParams = new URLSearchParams(location.hash.slice(1));
searchParams.delete(key);
location.hash = searchParams.toString();
},
};
export const useCounter = create()(
persist(
(set, get) => ({
count: 0,
increment: () => set({ count: get().count + 1 }),
decrement: () => set({ count: get().count - 1 }),
}),
{
name: "counter",
storage: createJSONStorage(() => hashStorage),
},
),
);create()((set) => ...) signature.createJSONStorage to keep your storage compliant with the StateStorage
interface.persist edge‑case fix.The hashStorage object keeps the persisted JSON state in the URL hash. That
makes the URL fully shareable and reload-safe. If your state is large, consider
persisting only a subset (via partialize) or switching to localStorage.
Now, when users interact with the useCounter store, the state will persist in the URL hash. For example, the URL might look like this:
https://saybackend.com#counter=%7B%22state%22%3A%7B%22count%22%3A1%7D%2C%22version%22%3A0%7D
Reloading or sharing the URL now restores the same counter state.
The custom StateStorage adapter is the whole trick: Zustand's persist
middleware writes JSON to the hash instead of localStorage. Keep the stored
slice small, because the URL is now part of your application's state format.
Discussion