All skills
asyrafhussin avatar

/state-management

@10bb16e

React Query and Zustand patterns for state management. Use when implementing data fetching, caching, mutations, or client-side state. Triggers on tasks involving useQuery, useMutation, Zustand stores, caching, or state management.

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/state-management

This session only. Nothing lands on disk.

rulesrq-query-keys.md

≈760 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Query Keys Best Practices

Impact: CRITICAL

Query keys are the foundation of React Query's caching and refetching system. They must be unique, serializable, and structured for effective cache management.

Bad Example

// Anti-pattern: Using primitive strings without structure
const { data: user } = useQuery({
  queryKey: ['user'],
  queryFn: () => fetchUser(userId),
});

const { data: posts } = useQuery({
  queryKey: ['posts'],
  queryFn: () => fetchUserPosts(userId, page),
});

// Anti-pattern: Inconsistent key structure
const { data: comments } = useQuery({
  queryKey: [`comments-${postId}`],
  queryFn: () => fetchComments(postId),
});

// Anti-pattern: Missing dependencies in key
const { data: filtered } = useQuery({
  queryKey: ['products'],
  queryFn: () => fetchProducts(category, sortBy, filters),
});

Good Example

// Create a query key factory for consistency
const userKeys = {
  all: ['users'] as const,
  lists: () => [...userKeys.all, 'list'] as const,
  list: (filters: UserFilters) => [...userKeys.lists(), filters] as const,
  details: () => [...userKeys.all, 'detail'] as const,
  detail: (id: string) => [...userKeys.details(), id] as const,
};

const postKeys = {
  all: ['posts'] as const,
  lists: () => [...postKeys.all, 'list'] as const,
  list: (userId: string, page: number) => [...postKeys.lists(), { userId, page }] as const,
  details: () => [...postKeys.all, 'detail'] as const,
  detail: (id: string) => [...postKeys.details(), id] as const,
  comments: (postId: string) => [...postKeys.detail(postId), 'comments'] as const,
};

// Usage with factory
const { data: user } = useQuery({
  queryKey: userKeys.detail(userId),
  queryFn: () => fetchUser(userId),
});

const { data: posts } = useQuery({
  queryKey: postKeys.list(userId, page),
  queryFn: () => fetchUserPosts(userId, page),
});

const { data: products } = useQuery({
  queryKey: ['products', { category, sortBy, filters }],
  queryFn: () => fetchProducts(category, sortBy, filters),
});

// Invalidation becomes easy
queryClient.invalidateQueries({ queryKey: userKeys.all });
queryClient.invalidateQueries({ queryKey: postKeys.lists() });

Why

  1. Cache Isolation: Structured keys ensure each unique data request has its own cache entry, preventing data collisions.

  2. Granular Invalidation: Hierarchical keys allow invalidating specific subsets of data (e.g., all user lists vs. a specific user detail).

  3. Automatic Refetching: When dependencies in the key change, React Query automatically refetches with the new parameters.

  4. Type Safety: Query key factories provide TypeScript support and prevent typos.

  5. Debugging: Consistent, structured keys make it easier to inspect and debug cache state in React Query DevTools.

  6. Maintainability: Centralized key definitions make refactoring easier and ensure consistency across the codebase.

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides secure and comprehensive documentation for managing application state using React Query and Zustand. It includes critical security warnings against storing sensitive data in local storage and promotes best practices for data fetching, caching, and error handling. No malicious patterns or security vulnerabilities were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    32 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 10bb16e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "1.1.0"
}

README badge

README badge for asyrafhussin/agent-skills/state-management