agentleFS
girijashankarjgirijashankarj/cursor-handbook/.cursor/rules/frontend/state-management.mdc

cursor-handbook / frontend

Cursor rule31 starsChanged 3 months agoMITView on GitHub

Frontend state management patterns and best practices. Apply when managing client state.

The file

We split this state-management.mdc into the rules it gives an agent

11 rules. About 0 are common, 9 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Keep state as close to where it's used as possible

    line 22
  2. Very specific:

    Use useReducer for complex state with multiple sub-values

    line 24
  3. Distinctive:

    Use TanStack Query (React Query) for all API calls

    line 27
  4. Distinctive:

    Use query keys that include all dependencies

    line 29
  5. Distinctive:

    Keep global state minimal — most state should be local or server

    line 49
  6. Distinctive:

    Use Zustand for lightweight global state

    line 50
  7. Distinctive:

    Use Redux Toolkit only if the app has complex client-side state

    line 51
  8. Distinctive:

    NEVER: put server data in global state — use React Query

    line 52
  9. Distinctive:

    Avoid unnecessary re-renders by keeping state granular

    line 55
  10. Distinctive:

    Use selectors for derived state

    line 56
  11. Very specific:

    Use React.memo for components that receive stable props

    line 58

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
state-management.mdc
---
description: "Frontend state management patterns and best practices. Apply when managing client state."
alwaysApply: false
globs: "**/*.tsx,**/*.jsx,**/store/**/*,**/state/**/*"
---

# State Management Rules

## State Categories

| Category | Tool | Example |
|----------|------|---------|
| **UI State** | `useState` / `useReducer` | Modal open, form input, toggle |
| **Shared UI State** | Context API / Zustand | Theme, sidebar, notifications |
| **Server State** | TanStack Query / SWR | API data, user profile, lists |
| **Form State** | React Hook Form | Form values, validation, submission |
| **URL State** | Router params / search params | Filters, pagination, selected tab |

## Rules

### Local State
- Keep state as close to where it's used as possible
- Lift state up only when siblings need to share it
- Use `useReducer` for complex state with multiple sub-values

### Server State
- Use TanStack Query (React Query) for all API calls
- Configure stale time and cache time appropriately
- Use query keys that include all dependencies
- Implement optimistic updates for better UX
- Handle loading, error, and empty states explicitly

```tsx
// CORRECT - Using TanStack Query
const { data, isLoading, error } = useQuery({
  queryKey: ['orders', filters],
  queryFn: () => fetchOrders(filters),
  staleTime: 5 * 60 * 1000, // 5 minutes
});

// WRONG - Manual fetch in useEffect
const [data, setData] = useState(null);
useEffect(() => {
  fetch('/api/orders').then(r => r.json()).then(setData);
}, []);
```

### Global State
- Keep global state minimal — most state should be local or server
- Use Zustand for lightweight global state
- Use Redux Toolkit only if the app has complex client-side state
- **NEVER** put server data in global state — use React Query

### Performance
- Avoid unnecessary re-renders by keeping state granular
- Use selectors for derived state
- Memoize expensive derived computations
- Use `React.memo` for components that receive stable props

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.