AI summary by TwinMind
The "Wrong Way" To Use React
Theo - t3․gg
39m 29s
Video Introduction and Context
- Video triggered by favorite author's blog post about data collocation in React components
- React data fetching model has become "messier and better"
- Created detailed diagram to clarify misconceptions about React data fetching patterns
- Covers difference between "fetch on render" vs "render as you fetch"
- Video includes code snippets, blog post analysis, and Twitter thread discussion
React 19 Suspense Drama Background
- React Community went through drama with React 19 changes
- Previously: React client concurrently rendered siblings under suspense boundary (like Promise.all)
- React 19 change: Sequential rendering of siblings under same suspense boundary
- Would display fallback faster but introduce waterfalls for racing tasks
- Big libraries affected: React Query, tRPC, React Three Fiber
- React team reverted changes after pushback
- Poll results: 50% not using React Query, 50% using React Query but not suspense query, only 2 people using suspense query
Fetch on Render Problem Demonstration
- Created mock example: FriendsList component fetching user IDs
- FriendItem component fetching individual friend data by ID
- Waterfall problem: Must load JS → render component → fetch data → rerender
- Each friend item makes separate request after friends list loads
- Beauty: Components can be mounted anywhere without props
- Cost: Can't optimize data loading, slow to get data to components
Backend/Frontend Reality
- Common issue: Backend teams don't provide complete data objects
- Frontend devs often work with 15 APIs from 50 different teams
- APIs don't return what's needed, forcing multiple fetch calls
- Realistic problem despite being "bad example"
Component Success vs Limitations
- React's fundamental bet: Components as primary UI building blocks
- Components contain state, logic, and view - wildly successful
- Problem: High cost of shared state across component tree
- State near root causes performance issues with large trees
- Data fetching in individual components leads to request waterfalls
Solutions and Ecosystem Response
- Community solutions: Flux, Redux, React Query, Relay, React Router
- Pattern: Need more than single component tree - managed state outside components
- Access via context and hooks
- Common structure: Component tree + ecosystem solutions
The Jungle Problem
- Quote from Joe Armstrong (Erlang creator): "Wanted banana, got gorilla holding banana and entire jungle"
- State collocation: Keep state close to where it's used
- Works for local state, expensive for shared state
- Workaround: Context providers with hooks (useSelector, useQuery, etc.)
- Cost: Components tightly coupled to global state and contexts
- Irony: More autonomous components become, less composable they are
Alternative Approaches
- Solid: Signals bypass state/view duality, view as byproduct
- Ryan Carniato declared "componentless future" in 2021
- Svelte 5 with runes, Vue Vapor moving away from runtime components
- HTMX: Locality of behavior principle, no real components
- State primarily server-side
React's Server Component Strategy
- React bringing components to server
- Server component tree manages server state/data sync
- Client gets new features: useOptimistic, useActionState, useFormStatus
- React implementing Flux pattern across network gap
- Client becoming leaner, React compiler making it faster
- Frameworks taking more central role
Tanner Lindsley Thread Analysis
- Concerned about React pushing "render as you fetch" as blessed approach
- Most React apps currently "fetch on render"
- Data collocation provides amazing DX
- Relay one of few libraries handling automatic data requirement hoisting
- RSC as spiritual evolution of Relay pattern
- Server-side waterfalls less problematic than client-side
Fetch Patterns Comparison Diagram
- Fetch on Render: Client requests HTML → fetch JS → render → needs user info → fetch user info → render friends list → needs friends data → fetch friends → render individual friends → needs friend details → fetch friend details
- Render as You Fetch: Client requests HTML → server simultaneously starts all data fetching → streams responses when ready → components render as data arrives
- Key difference: Render triggers fetch vs Fetch triggers render
- Render as you fetch exponentially faster due to parallel processing and single authentication
Technical Implementation Challenges
- RSC requires significant architecture changes beyond "npm install react"
- Checklist includes: data hoisting, compiler, router conventions, cache coordinator, consumer patterns, SSR streaming hydration
- More performant for some KPIs but not everyone needs it
- Tanner advocates for retaining collocation DX without forcing prefetching cognitive load
Future Considerations
- Need React to consider collocation as first-class instead of "cursed pattern"
- TanStack Query working on RSC compatibility
- Goal: Reduce cognitive load around pre-loading decisions
- "In the end we're all reinventing Relay"
