AI summary by TwinMind
You still use Redux?
Theo - t3․gg
36m 39s
Opening: Core State Categories
- State types breakdown: server state vs application/client state
- Demo example: onblur mutation → revalidate query pattern
- Main thesis: React state management over-discussed, solutions under-discussed
- Key principle: "If you can use React Query, you probably should"
Server State vs Client State Distinction
- Server state: user profiles, friend counts, live Twitch users, data from backend
- Client state: current page, microphone on/off, video device connection, menu open/closed
- Most "state management" problems = state synchronization between client/server
- Common mistake: reaching for Redux/Zustand when need server synchronization tool
- Recommendation: read TK Dodo's Practical React Query guide first
React Query Implementation Demo
- Server state example:
const {data, isLoading} = useQuery(['some-query'], fetchData) - Client state example:
useStatefor menu open/closed - Problem: using server data as input
valueprevents editing - Solution 1:
defaultValuedoesn't work - decides on first render when data undefined - Solution 2: conditional rendering with
isLoadingcheck prevents premature render - Mutation pattern:
const mutate = useMutation(data => updateContent(data)) - Input handling:
onBlur={e => mutate(e.currentTarget.value)} - Revalidation:
onSuccess: () => refetch()orqueryClient.invalidateQueries(['some-query'])
Advanced React Query Patterns
- Optimistic updates:
onMutateruns before server response, immediate UI update - Cache updates:
queryClient.setQueryData()to manually update cache - Loading states:
isLoadingvsisFetchingfor different UX patterns - Fake server delay demo with
setTimeoutin promise - Multiple strategies combinable based on UX requirements
What Isn't Server State
- AV device management
- Navigation state
- Open/close menu state
- Chat visibility/global notifications
- Camera device instantiation (async but not server-dependent)
- React Query rating: good for AV (not great), bad for navigation, terrible for menu/chat state
Built-in React State Solutions
- Primary recommendation:
useState+ prop drilling - Don't fear prop drilling - passing props is fine
- Context caveat: good for dependency injection, bad for changing values
- Context problem: everything consuming context renders when any value changes
- React.memo doesn't solve context re-render issues, doubles computation
- Context rule: use for static data from "here down", not for frequently changing state
State Management Decision Tree
When useState Doesn't Work:
- Need Global Access → Jotai
- Atomic state management
const countAtom = atom(0)const [count, setCount] = useAtom(countAtom)- Reusable across 100+ places, only consumers of changed atoms re-render
- Efficient alternative to context with multiple values
- Need Actions/Events/Machine → Zustand
- Store-based with defined actions
create((set) => ({ bears: 0, increase: () => set(state => ({bears: state.bears + 1})) }))- Selective subscriptions:
useStore(state => state.bears)only renders when bears changes - External access: can call outside React components
- Good for websocket connections, device management
Alternative Solutions Comparison
- Recoil: similar to Jotai, but Jotai simpler/better maintained
- Redux: overkill for most use cases
- Jotai: 3.39kb, Zustand: 1.16kb vs Redux's larger size
- Redux does more than needed for typical applications
- XState: formal state machine architecture
- 39 lines of code for simple fetch
- Only use for extremely complex state with need for visual diagrams
- Warning: introduces complexity, use when complexity makes reasoning simpler
- Issue: sibling machine communication difficulties
Real-world Usage Patterns
- Most applications: React Query handles majority of state needs
- Ping.gg examples:
- Jotai: global device management, active device tracking
- Zustand: chat room management, websocket connections, external listeners
- Everything else: React Query for server synchronization
- Philosophy: start simple (useState + React Query) → add Jotai/Zustand when hitting specific limits
