Zustand for Next.js App Router - Client Components only, hydration handling, persist middleware. Use when implementing global state in Next.js.
View on GitHubfusengine/agents
fuse-nextjs
plugins/nextjs-expert/skills/nextjs-zustand/SKILL.md
January 22, 2026
Select agents to install to:
npx add-skill https://github.com/fusengine/agents/blob/main/plugins/nextjs-expert/skills/nextjs-zustand/SKILL.md -a claude-code --skill nextjs-zustandInstallation paths:
.claude/skills/nextjs-zustand/# Zustand for Next.js
State management for Next.js Client Components.
## Installation
```bash
bun add zustand
```
---
## Important: Client Components Only
Zustand stores work in **Client Components only**. Server Components cannot use hooks.
```typescript
// ❌ BAD - Server Component
// app/page.tsx
export default function Page() {
const count = useCounterStore((s) => s.count) // Error!
}
// ✅ GOOD - Client Component
// app/Counter.tsx
'use client'
export function Counter() {
const count = useCounterStore((s) => s.count)
}
```
---
## Basic Store
```typescript
// stores/useCounterStore.ts
import { create } from 'zustand'
interface CounterState {
count: number
increment: () => void
decrement: () => void
reset: () => void
}
export const useCounterStore = create<CounterState>((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
reset: () => set({ count: 0 }),
}))
```
---
## Store with Async Actions
```typescript
// stores/useUserStore.ts
import { create } from 'zustand'
interface User {
id: string
name: string
email: string
}
interface UserState {
user: User | null
loading: boolean
error: string | null
fetchUser: (id: string) => Promise<void>
logout: () => void
}
export const useUserStore = create<UserState>((set) => ({
user: null,
loading: false,
error: null,
fetchUser: async (id) => {
set({ loading: true, error: null })
try {
const res = await fetch(`/api/users/${id}`)
if (!res.ok) throw new Error('Failed to fetch')
const user = await res.json()
set({ user, loading: false })
} catch (err) {
set({ error: (err as Error).message, loading: false })
}
},
logout: () => set({ user: null }),
}))
```
---
## Persist with SSR Hydration
```typescript
// stores/useAuthStore.ts
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'