본문으로 건너뛰기
cccskills
무료GitHub에 공개

state-management-patterns

When deciding where and how to store data in a modern web application.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

설치 방법 보기

포함된 파일(1)

  • SKILL.md6.9 KB

SKILL.md(원문)

설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.

State Management Patterns

Purpose

Applications suffer from bugs and performance issues when state lives in the wrong place. This skill guides teams to categorize state by its nature (UI, server, URL, global) and store it appropriately, eliminating duplication, excessive re-renders, and synchronization bugs.

When to use

  • Adding new interactive features that require data persistence across views
  • Refactoring an app suffering from excessive re-renders
  • Integrating external data fetching with local UI state
  • Deciding where a new piece of state should live
  • Migrating from Redux/global store when unnecessary

When NOT to use

  • Component-level prop decisions (use React Component Design skill)
  • Server caching strategy decisions (use Caching Strategies skill)
  • Real-time synchronization protocols (different concern)

Inputs required

  • Existing React application with state management code
  • Understanding of current state locations (Redux, Context, useState)
  • List of state mutations and their trigger sources

Workflow

  1. Categorize State: Classify each state piece as: UI-only, Server, URL-driven, or Truly Global
  2. URL First: Move ALL sort, filter, pagination, search, and tab parameters to URL search params
  3. Separate Server Cache: Use React Query / SWR / Tanstack Query for ALL server data (never Redux)
  4. Localize UI State: Keep form inputs, modals, toggles, tooltips in component useState only
  5. Identify Global: ONLY theme, auth, and language user preferences go to Context/Redux
  6. Implement Separation: Refactor code to respect these boundaries
  7. Verify Sync: Ensure no state is duplicated across layers

Rules

  • MUST store shareable states (filters, search, pagination) in URL, not Redux/Context
  • MUST use React Query/SWR for server data, NEVER Redux
  • MUST NOT manually sync server data between global store and cache layer
  • MUST NOT store UI-only state (modal open, form focus) in global store
  • MUST NOT mirror props into local state
  • MUST NOT have duplicate copies of state

Anti-patterns

  • Mirroring State: const [val, setVal] = useState(props.val) (causes sync bugs)
  • Global Everything: Redux store bloated with UI toggles and temporary form data
  • Manual Server Sync: Fetching data and manually updating Redux instead of using React Query
  • URL-less Navigation: Using only React Router state for filters/search instead of URL params
  • Inverted Cache: Using Redux as the source of truth for server data instead of cache layer

Failure conditions

  • Attempting to manually sync server data to global store
  • UI-only state (modals, toggles) stored in Redux
  • State duplication across URL, cache, and store
  • Search/filter parameters not reflected in URL

Validation checklist

  • All shareable states (filters, search) are in URL search params
  • Server data is fetched via React Query / SWR, not Redux
  • No manual synchronization between global store and cache layer
  • UI-only state (modals, tooltips, focus) is in component useState
  • Global store contains ONLY: auth, theme, language, user preferences
  • No prop-value mirroring in useState
  • URL parameters drive filtering/sorting in data display
  • Back button behavior works correctly with filter/search state

Output format

  • Code structure: Separate imports for URL hooks, server cache hooks, and Context providers
  • File organization: State logic in custom hooks, not component bodies
  • API shape: URL params in component mount, server cache managed by React Query
  • Validation: Backward-compatible URL parameter parsing with defaults

Security considerations

  • URL state MUST be validated/sanitized before use (filter injection)
  • Sensitive data (auth tokens, PII) MUST NEVER be in URL
  • Server cache MUST respect user permissions before caching
  • Global store credentials MUST be HttpOnly/secure if stored

Agent execution notes

  • Agent MAY: Refactor state to new locations, create custom hooks for state logic, migrate Redux to React Query
  • Agent MUST NEVER: Store auth tokens in URL, put sensitive PII in URL, manually sync server cache
  • Agent MUST ASK: Before deleting Redux/Context entirely, before moving state to global store
  • Agent MUST VALIDATE: URL state validated, no state duplication, React Query properly configured

Example

❌ Anti-pattern (Wrong state locations, duplication, mirroring):

// Redux store has everything including UI state
const store = {
  filters: { search: '', sort: 'name' },   // WRONG: belongs in URL
  users: [...],                             // OK: server data
  isModalOpen: false,                       // WRONG: UI-only state
  theme: 'dark'                             // OK: truly global
};

// Component mirrors props and stores server data locally
const UserList = ({ searchQuery }) => {
  const [search, setSearch] = useState(searchQuery);  // ANTI-PATTERN: mirrors props
  const [users, setUsers] = useState([]);              // ANTI-PATTERN: manual server sync
  
  useEffect(() => {
    fetch(`/api/users?q=${search}`)
      .then(res => res.json())
      .then(data => setUsers(data));  // Manual sync to local state
  }, [search]);
  
  return <div>{users.map(u => <p>{u.name}</p>)}</div>;
};

✅ Correct pattern (Proper state separation):

const UserList = () => {
  // 1. URL drives state (filters/search)
  const [searchParams, setSearchParams] = useSearchParams();
  const search = searchParams.get('q') || '';
  
  // 2. Server data via React Query (cached, not Redux)
  const { data: users } = useQuery({
    queryKey: ['users', search],
    queryFn: () => fetch(`/api/users?q=${search}`).then(r => r.json())
  });
  
  // 3. UI-only state stays local
  const [isModalOpen, setIsModalOpen] = useState(false);
  
  // 4. Global state from Context/Redux
  const { theme } = useContext(ThemeContext);
  
  return (
    <div>
      <input
        value={search}
        onChange={(e) => setSearchParams({ q: e.target.value })}
      />
      <button onClick={() => setIsModalOpen(true)}>Add User</button>
      {users?.map(u => <p>{u.name}</p>)}
    </div>
  );
};

리뷰

아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.

댓글

로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.

아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.

보내면 바로 공개됩니다. 이메일 주소나 전화번호 같은 개인 정보는 쓰지 마세요. URL은 2개까지입니다.

0 / 1,000자

이 양식은 reCAPTCHA로 보호되며 Google 개인정보처리방침 및 서비스 약관이 적용됩니다.

같은 저장소의 스킬

When designing and implementing web interfaces to ensure they are usable by everyone.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

KraitDev/skiLL.Md62026년 6월 8일 업데이트

When creating or extending an HTTP API for client consumption.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

KraitDev/skiLL.Md62026년 6월 8일 업데이트

When improving read performance and reducing database load.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

KraitDev/skiLL.Md62026년 6월 8일 업데이트

When configuring automated build, test, and deployment workflows for a repository.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

KraitDev/skiLL.Md62026년 6월 8일 업데이트

When asynchronously reviewing peer code before merging into the main branch.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

KraitDev/skiLL.Md62026년 6월 8일 업데이트

When addressing slow application endpoints, high database CPU usage, or standardizing data access patterns.

한국어 요약은 준비 중입니다. 원문 설명을 표시하고 있습니다.

KraitDev/skiLL.Md62026년 6월 8일 업데이트

KraitDev의 스킬 모두 보기

이 스킬의 문제 신고