아래 다이어그램은 useQuery로 가져온 데이터가 어떤 상태를 거쳐 관리되고 사라지는 지를 보여줍니다.

stateDiagram-v2
    classDef fresh fill:#15803d,stroke:#0f5027,stroke-width:2px,color:#fff
    classDef fetching fill:#1d4ed8,stroke:#173d9e,stroke-width:2px,color:#fff
    classDef stale fill:#a16207,stroke:#6d4004,stroke-width:2px,color:#fff
    classDef inactive fill:#525252,stroke:#333,stroke-width:2px,color:#fff
    classDef error fill:#b91c1c,stroke:#7d1010,stroke-width:2px,color:#fff

    [*] --> fetching: 쿼리 인스턴스 마운트
    fetching --> fresh: 로딩 성공
    fetching --> error: 재시도 모두 실패
    
    fresh --> stale: staleTime 경과
    stale --> fetching: 자동 refetch 트리거

    fresh --> inactive: 컴포넌트 언마운트
    stale --> inactive: 컴포넌트 언마운트
    
    error --> fetching: 수동 재시도

    inactive --> stale: 동일 쿼리 재마운트
    inactive --> [*]: gcTime 경과 후 삭제

    note right of fetching
        데이터를 가져오는 중...
        (기본 3회 자동 재시도)
    end note

    note right of fresh
        "신선한" 데이터 상태.
        staleTime(기본 0ms)이
        지나기 전까지 유지됨.
    end note

    note right of stale
        "오래된" 데이터 상태.
        데이터는 캐시에 있지만,
        특정 조건에서 refetch 됨.
    end note

    note right of inactive
        사용하는 컴포넌트가 없음.
        캐시에 데이터는 남아있지만,
        gcTime(기본 5분) 후 삭제 대상이 됨.
    end note

    class fresh fresh
    class fetching fetching
    class stale stale
    class inactive inactive
    class error error