Overview
AnimeThemes Web uses a multi-layered state management approach, combining different tools for different types of state:React Query
Server state for data fetching and caching
SWR
Authentication state with automatic revalidation
React Context
Global UI state (player, theme, toasts)
State Architecture
React Query (@tanstack/react-query)
Setup
React Query is initialized in_app.tsx:
Usage Pattern
Components useuseQuery for data fetching:
- Recently Added Videos
- Search Results
Benefits
Automatic Caching
Queries are cached by their
queryKey, preventing duplicate requestsLoading States
isLoading, isError, and isFetching states built-inStale-While-Revalidate
Shows cached data while fetching fresh data in background
Placeholder Data
Show skeleton states before data loads
SWR
Authentication State
SWR is used specifically for authentication because it provides excellent auto-revalidation:React Context
Context Providers
Multiple contexts are stacked using a custom provider pattern:Player Context
Manages video player state and watch list:Player Context Implementation
Player Context Implementation
Color Theme Context
Manages dark/light theme preference:Toast Context
Manages notification toasts:Local Storage State
Persistent local state usesuse-local-storage-state:
| Key | Purpose | Type |
|---|---|---|
color-theme | User theme preference | "light" | "dark" | "system" |
auto-play | Global auto-play setting | boolean |
watch-history | Recently watched videos | Array<{ videoId: number; timestamp: number }> |
anime-filter-{page} | Filter preferences per page | Record<string, string> |
Custom Hooks
useCurrentSeason
useWatchHistory
State Management Flow
Best Practices
Use React Query for Server State
Data from APIs should be managed with React Query for automatic caching and revalidation
Use SWR for Auth
Authentication state benefits from SWR’s automatic revalidation on focus
Use Context Sparingly
Only use Context for truly global state (theme, player, toasts). Avoid prop-drilling, but don’t over-use Context
Persist User Preferences
Use local storage for theme, auto-play, filters, and other user preferences
Co-locate State
Keep state as close to where it’s used as possible. Not everything needs to be global
Type Everything
Use TypeScript interfaces for all state shapes to ensure type safety
State Debugging
React Query DevTools
React Query DevTools
Logging Context Changes
Logging Context Changes