state-management

Guides choosing between SWR, Zustand, and React Context for provider-scoped state in React.

10|3|Updated Oct 23, 2023
One-click install
npx skills add https://github.com/Layer-Fi/layer-react --skill state-management-layer-fi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: state-management
Source: https://github.com/Layer-Fi/layer-react/tree/main/src/providers
Command: npx skills add https://github.com/Layer-Fi/layer-react --skill state-management-layer-fi

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires zustand, swr, react-i18next, react-intl, react-aria-components.

What problem does it solve? React codebases often misuse state tools, putting server data in stores or frequently-changing state in Context, causing stale data, re-render storms, and state leaking between mounted component instances. ## Core Features & Use Cases - Tool Selection Rules: Assigns server state to SWR, shared client state to Zustand, and dependency injection to React Context, with concrete examples from the codebase. - Provider-Scoped Store Pattern: Defines the canonical Zustand store shape (context-supplied store built once per provider, narrow selector hooks, split state/action selectors) so multiple mounted instances never share state. - Layered Provider Layout: Enforces the global/common vs features directory split, one-concern-per-context composition, and narrow store-to-server reconciliation rules. - Use Case: When adding a new feature with filters, pagination, and selections, follow this Skill to create small provider-scoped Zustand stores and DI contexts instead of one fat context that re-renders every consumer. ## Quick Start Ask the AI to design the state management for a new feature in src/providers following the state-management skill rules.

Frequently Asked Questions about state-management

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose between SWR, Zustand, and React Context in React?▼

Use SWR for server state like fetching and caching, Zustand for client state shared across a feature subtree such as filters and selections, and React Context for dependency injection of stable values like auth, environment, and locale.

How do I create a provider-scoped Zustand store in React?▼

Create a context holding an inert default store, build the real store once in the provider with useState(() => createStore(...)), and expose narrow selector hooks using useStore. This prevents state leaking between multiple mounted instances of the same component.

Why should frequently-changing state not live in React Context?▼

Every consumer of a context re-renders when its value changes, so holding rapidly-updating state in context causes widespread re-renders. Use a Zustand store with selector hooks instead so components only re-render when their selected slice changes.

Can I copy server data from SWR into a Zustand store?▼

No, server data should live only in the SWR cache and be read through hooks at the point of use. Stores may only narrowly reconcile, such as pruning selected IDs no longer in the server list or seeding an initial value once.

When should I use createScopedStore instead of a hand-rolled provider?▼

createScopedStore is a generic factory that throws when hooks are used outside its provider, rather than falling back to a dead default store. Today only the date store uses it; the hand-rolled context-plus-createStore shape remains the prevailing idiom for feature stores.