agentleFS
roborracleroborracle/agent-workflow-kit/skills/state-management/SKILL.md

state-management

Skill0 starsChanged 3 months agoMITView on GitHub

Design frontend state architecture. Covers state categorization (server vs client vs URL), library selection, cache invalidation, optimistic updates, and state machine patterns.

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 8 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

  • 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

  • 0xSeroSkill

    security

    Also in 4 projects that use this

The file

We split this skill into the rules it gives an agent

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

  • 0

    Common

    Most of the 21 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before choosing a library, classify every piece of state:

    line 11
  2. Distinctive:

    Use discriminated unions to make invalid states unrepresentable.

    line 99

“Common” is measured: kept by at least 6 in 10 of the 21 edited versions of this file we could read. “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
SKILL.md

State Management Architect

State Categorization

Before choosing a library, classify every piece of state:

CategorySource of TruthPersistenceExamples
Server stateDatabase/APISurvives refreshUser profile, orders, products
Client stateBrowser memoryLost on refreshModal open/closed, selected tab, sidebar collapsed
URL stateURL barShareable/bookmarkableCurrent page, filters, search query, sort order
Form stateInput elementsUntil submitField values, validation errors, dirty tracking

Rule: Use the right tool for each category

CategoryRecommended ToolAnti-Pattern
Server stateTanStack Query / SWRRedux store with manual fetch
Client stateuseState / Zustand / JotaiContext for frequently changing values
URL stateURL search params / routerRedux or useState for page/filter state
Form stateReact Hook Form / nativeGlobal store for form values

Server State (TanStack Query Pattern)

// Fetch with automatic caching, refetching, and loading states
const { data, isLoading, error } = useQuery({
  queryKey: ['users', userId],
  queryFn: () => api.getUser(userId),
  staleTime: 5 * 60 * 1000, // Consider fresh for 5 minutes
});

// Mutation with optimistic update
const mutation = useMutation({
  mutationFn: api.updateUser,
  onMutate: async (newData) => {
    await queryClient.cancelQueries({ queryKey: ['users', userId] });
    const previous = queryClient.getQueryData(['users', userId]);
    queryClient.setQueryData(['users', userId], newData);
    return { previous };
  },
  onError: (err, vars, ctx) => queryClient.setQueryData(['users', userId], ctx.previous),
  onSettled: () => queryClient.invalidateQueries({ queryKey: ['users', userId] }),
});

Client State (Zustand Pattern)

const useUIStore = create<UIState>((set) => ({
  sidebarOpen: false,
  theme: 'light',
  toggleSidebar: () => set((s) => ({ sidebarOpen: !s.sidebarOpen })),
  setTheme: (theme) => set({ theme }),
}));
// Usage: subscribe to slices — const open = useUIStore((s) => s.sidebarOpen);

URL State Pattern

// Filters and pagination belong in the URL, not in state
const [searchParams, setSearchParams] = useSearchParams();
const page = parseInt(searchParams.get('page') || '1', 10);
const sort = searchParams.get('sort') || 'newest';
const filter = searchParams.get('filter') || 'all';

Cache Invalidation Strategy

EventInvalidate
Create itemList queries for that resource
Update itemItem query + list queries
Delete itemList queries (remove from cache directly)
Bulk operationAll queries for that resource type
Background (polling)Stale queries on window focus

State Machine Pattern (Complex Flows)

For multi-step workflows (checkout, onboarding, file upload):

type UploadState =
  | { status: 'idle' }
  | { status: 'selecting' }
  | { status: 'uploading'; progress: number }
  | { status: 'processing' }
  | { status: 'complete'; url: string }
  | { status: 'error'; message: string };

Use discriminated unions to make invalid states unrepresentable.

Anti-Patterns

  • Global state for everything (makes components impossible to test in isolation)
  • Duplicating server data in client store (stale by definition)
  • Prop drilling through 5+ levels (use composition or context for stable values)
  • Context for frequently changing values (re-renders entire tree)
  • Derived state stored separately (compute it from source, don't sync it)
  • useEffect to sync state between two useState calls (combine into one state or use derived value)

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.