> nextjs-state-management
Best practices for managing state (Server URL vs Client Hooks). Use when managing URL state, client state, or global state in a Next.js application. (triggers: **/hooks/*.ts, **/store.ts, **/components/*.tsx, useState, useContext, zustand, redux)
curl "https://skillshub.wtf/HoangNguyen0403/agent-skills-standard/nextjs-state-management?format=md"State Management
Priority: P2 (MEDIUM)
Principles
- URL as Source of Truth: For shareable/persistent state (Search, Filters, Pagination), use URL params with
useSearchParams. - Colocation: Keep state close to components. Lift only when sharing between siblings.
- No Global Store Default: Avoid Redux/Zustand for simple apps. Use only for complex cross-cutting concerns (Music Player, Shopping Cart).
Patterns
1. Granular State (Best Practice)
Don't store large objects. Subscribe only to what you need to prevent unnecessary re-renders.
// BAD: Re-renders on any change to 'user'
const [user, setUser] = useState({ name: '', stats: {}, friends: [] });
// GOOD: Independent states
const [name, setName] = useState('');
const [stats, setStats] = useState({});
2. URL-Driven State (Search/Filter)
// Client Component
'use client';
import { useSearchParams, useRouter, usePathname } from 'next/navigation';
export function Search() {
const searchParams = useSearchParams();
const { replace } = useRouter();
const pathname = usePathname();
function handleSearch(term: string) {
const params = new URLSearchParams(searchParams);
if (term) params.set('q', term);
else params.delete('q');
// Updates URL -> Server Component re-renders with new params
replace(`${pathname}?${params.toString()}`);
}
}
3. Server State (TanStack Query / SWR)
If you need "Live" data on the client (e.g., polling stock prices, chat), do not implement useEffect fetch manually. Use a library.
// Automated caching, deduplication, and revalidation
const { data, error } = useSWR('/api/user', fetcher);
Library Patterns
For specific state management patterns, see:
🚫 Anti-Patterns
- Do NOT use standard patterns if specific project rules exist.
- Do NOT ignore error handling or edge cases.
> related_skills --same-repo
> typescript-tooling
Development tools, linting, and build config for TypeScript. Use when configuring ESLint, Prettier, Jest, Vitest, tsconfig, or any TS build tooling. (triggers: tsconfig.json, .eslintrc.*, jest.config.*, package.json, eslint, prettier, jest, vitest, build, compile, lint)
> typescript-security
Secure coding practices for TypeScript. Use when validating input, handling auth tokens, sanitizing data, or managing secrets and sensitive configuration. (triggers: **/*.ts, **/*.tsx, validate, sanitize, xss, injection, auth, password, secret, token)
> typescript-language
Modern TypeScript standards for type safety and maintainability. Use when working with types, interfaces, generics, enums, unions, or tsconfig settings. (triggers: **/*.ts, **/*.tsx, tsconfig.json, type, interface, generic, enum, union, intersection, readonly, const, namespace)
> typescript-best-practices
Idiomatic TypeScript patterns for clean, maintainable code. Use when writing or refactoring TypeScript classes, functions, modules, or async logic. (triggers: **/*.ts, **/*.tsx, class, function, module, import, export, async, promise)