Skip to content

skillFrontendreact

React State Management

Chooses local state, context, a reducer or a store for each piece of React state and removes prop drilling without adding a library by habit.

npx workspacetemplates add react-state-management --tool claude-code

Writes 1 file to .claude/skills/react-state-management/

.claude/skills/react-state-management/SKILL.md32 lines · 1.8 KBFile
---
name: react-state-management
description: Chooses local state, context, a reducer or a store for each piece of React state and removes prop drilling without adding a library by habit.
---

# React state management

Put state in the lowest place that works. Add machinery only when that fails.

## Steps

1. Inventory the state. List each piece with its owner, readers and source. Classify it: server data (from an API), URL state (filters, page, tab), form state, UI state (open, hover), or true app state (session, theme, cart).
2. Choose the home for each kind:
   - Server data goes to a query library such as TanStack Query or SWR, not to a store. Cache, refetch and invalidate there.
   - URL state lives in the router or search params so links and the back button work.
   - Form state stays in the form, or a form library for large forms.
   - UI state stays local with `useState`, lifted only to the nearest common parent.
   - Complex transitions with several related values use `useReducer` with a typed action union.
   - App state shared by distant components goes to context for rarely changing values, or a small store (Zustand, Jotai, Redux Toolkit) when many components read changing values.
3. Remove prop drilling by composition first: pass components or `children` instead of data through layers. Add context after that.
4. Split contexts by concern and by update rate. Memoize the provider value. Expose a custom hook that throws outside the provider.
5. Do not store derived data; compute it. Keep state normalized and minimal.
6. Migrate one slice per commit and run the tests after each.

## Rules

- No new dependency without saying what it replaces.
- Never mirror props into state.

## Report

A table of state, old home, new home and reason, plus what was left alone.

What it does

Teams reach for a global store on day one, then wonder why every change touches five files. This skill puts each piece of state where it belongs: as close to its users as possible, and in the server cache if it came from a server.

The agent sorts the state it finds into kinds, picks the lightest tool for each, and migrates one slice at a time so the app keeps working between commits.

Example

you › Cart data is passed through 5 components and our Redux store holds fetched products

agent › Moved products to TanStack Query (3 hooks, cache time 5 minutes) and deleted the products slice. Cart stays global: a 40-line Zustand store with selectors, since 6 distant components read it. Filters moved to search params. Drilled props removed from 5 components; all 58 tests pass.

Questions

Will it install a state library?
Only if no lighter option fits, and it says which dependency it adds and what it replaces. Many apps need none.
Does it rewrite everything at once?
No. It migrates one slice per commit so you can review and revert each step.
  • Bundle Size Reduction

    Skilljavascript

    Free