agentleFS
manish1803manish1803/nextjs-fullstack-skills/skills/02-fullstack-backend/state-management/SKILL.md

state-management

Skill2 starsChanged 4 months agoMITView on GitHub

Choose and implement the right state management approach in React/Next.js — server state (React Query/SWR), global client state (Zustand/Context), URL state, and form state. Use whenever the user asks "where should this state live", introduces prop-drilling pain, asks about Redux/Zustand/Context/React Query, or is deciding how to share state across components.

27 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use css-architecture, error-handling and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • Piyush8296Skill

    css-architecture

    CSS architecture, Tailwind patterns, responsive design, design tokens, and styling best practices. Use when implementing designs, setting up styling infrastructure, or debugging layout issues.

    Also in 3 projects that use this

  • MicrosoftSkill

    error-handling

    统一错误处理系统。在添加 API 端点、修改错误处理、添加前端 API 调用、编写错误相关测试时使用。

    Also in 6 projects that use this

  • lbednerSkill

    i18n

    Use when a code or template change introduces a new translation message key, in either the framework CLI (aegis/i18n/locales/) or a generated project's CLI (the app/i18n/locales/ template tree). Covers adding the real English string, stubbing the same key into every other locale for parity, and the separate translation PR that later replaces placeholders with reviewed native text.

    Also in 4 projects that use this

  • mijunriSkill

    database

    数据库脚本与操作规范。脚本放在 db_scripts 目录,遵循查询优化、事务、安全等实践。

    Also in 3 projects that use this

  • genomewalkerSkill

    resume

    Resume a thread by loading its ~800-token context capsule

    Also in 3 projects that use this

  • NVIDIASkill

    testing

    Run Python and Rust tests for physicsnemo-curator using uv + pytest with coverage reporting via pytest-cov, and cargo-nextest for Rust tests. Includes benchmark workflows with pytest-benchmark and criterion.

    Also in 6 projects that use this

The file

Files
SKILL.md

State Management Skill

Decision framework: classify the state before picking a tool

  1. Server state (data that lives in a database, fetched over the network — products, user profile, orders): use TanStack Query (React Query) or SWR, or in Next.js, Server Components + Server Actions with revalidatePath/revalidateTag. Never mirror server data into a global client store (Redux/Zustand) — you'll fight cache invalidation forever.
  2. URL state (filters, pagination, selected tab, search query — anything that should survive a refresh/be shareable/bookmarkable): keep it in the URL (useSearchParams/nuqs), not component state.
  3. Global client-only state (theme, sidebar open/closed, multi-step wizard progress not tied to a URL, cart contents before checkout): Zustand for anything beyond trivial — lighter and less boilerplate than Redux for most app sizes; React Context only for low-frequency-update, small-scope state (e.g. auth user object, theme) since Context re-renders all consumers on every change.
  4. Local component state: plain useState/useReducer — the default, don't reach for global state until 2+ unrelated components genuinely need the same value.

When Redux Toolkit is still the right call

Large teams with complex, deeply interdependent client state, time-travel debugging needs, or an existing Redux codebase. For most new MERN/Next.js projects in 2026, Zustand + React Query covers the same ground with far less boilerplate — default to that combination unless there's a specific reason for Redux.

Zustand pattern

const useCartStore = create<CartState>((set) => ({
  items: [],
  addItem: (item) => set((state) => ({ items: [...state.items, item] })),
  removeItem: (id) => set((state) => ({ items: state.items.filter(i => i.id !== id) })),
}));

Select narrowly in components (useCartStore(s => s.items)) rather than destructuring the whole store, to avoid unnecessary re-renders.

React Query pattern (server state)

const { data, isLoading } = useQuery({ queryKey: ['products', filters], queryFn: () => fetchProducts(filters) });
const mutation = useMutation({ mutationFn: updateProduct, onSuccess: () => queryClient.invalidateQueries({ queryKey: ['products'] }) });

In a Next.js App Router app with Server Components as the primary data source, reach for React Query mainly for client-side interactive data (search-as-you-type, optimistic updates, polling) rather than initial page data.

Anti-patterns to flag

  • Server-fetched data duplicated into Redux/Zustand "just in case," creating two sources of truth
  • Filter/pagination state kept in useState instead of the URL, breaking back-button and shareable links
  • Context used for high-frequency-updating state (e.g. mouse position, form input on every keystroke) causing broad re-renders
  • Prop-drilling 4+ levels instead of composition or a scoped store

Example prompt this skill should trigger on

"I'm prop-drilling the cart state through 5 components, what's the right way to manage this?"

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.