AI summary by TwinMind
“Goodbye useEffect” Reaction
Theo - t3․gg
55m 48s
Opening React and UseEffect Discussion
- Speaker discussing useEffect as "everyone's favorite" hook (sarcastically)
- Personal experience with useEffect causing problems since starting with React hooks
- Acknowledges even senior React developers struggle with useEffect
- Main thesis: "useEffect is not for effects"
- Mentions useEvent could solve about 1/3 of useEffect complaints
- Another 1/3 could be solved by async libraries like React Query
- Final 1/3 are genuine ergonomic issues
Class Components vs Hooks Comparison
- Class components had 3 places for effects:
- componentDidMount
- componentDidUpdate (when state/props change)
- componentWillUnmount (cleanup/unsubscriptions)
- UseEffect with empty dependency array mimics componentDidMount
- Problem: complex logic duplication across lifecycle methods
- Example: ChatBox component with slug prop
- Mount: subscribe using slug
- Update: needs unmount + remount logic
- Unmount: cleanup subscription
- Results in duplicated connection/disconnection logic
- Additional props (like dark mode) cause unnecessary reconnects without proper comparison
Theo's Commentary on Class Components
- Strongly disagrees with presenter's framing
- Class component lifecycle methods were fundamentally bad
- UseEffect provides more granular control over when actions occur
- Criticism of "bad faith" comparison between old and new patterns
- Argues community has accepted old component model was problematic
- Claims most useEffect complaints come from "incompetent engineers"
- Suggests useEffect should require dependency array parameter
UseEffect Patterns and Problems
- Default behavior of useEffect is infinite loop
- Common mistake: missing dependency arrays
- React documentation shows examples without dependency arrays initially
- Dependency arrays introduced as "tip" at bottom of docs
- Recommendation: wrap useEffect in custom hooks with descriptive names
- Avoid multiple effects per component
UseEffect vs Lifecycle Mental Model
- UseEffect is NOT a lifecycle hook
- React 18 makes this clear with strict mode double execution
- Mount → Effect → Unmount → Remount → Effect (in strict mode)
- Purpose: detect effects that shouldn't be there
- UseEffect should be called "useSynchronize"
Proper UseEffect Usage - Synchronization
- UseEffect designed for synchronization with external systems
- Examples:
- Subscriptions
- DOM event listeners (mouse events, etc.)
- Cleanup in return function
- Keep effects small and focused
- Two types of effects:
- Synchronized effects (subscriptions, event handlers)
- Action effects (fire-and-forget)
Action Effects and Event Handlers
- Action effects should go in event handlers (onClick, onSubmit)
- React 18 strict mode will execute effects twice on mount
- Event handlers guarantee single execution per state transition
- Official React documentation: "event handlers are best place for side effects"
- UseEffect should be "last resort"
Theo's Twitch Dashboard Bug Story
- Bug where opening dashboard triggered ads immediately
- Root cause: action bound to component mount instead of button click
- State persisted in localStorage including loading state
- Component named "action" had onMount effect that fired action
- Bad localStorage state would trigger ads on page load
- Bug affected multiple channels if user was admin
- Took 2+ hours to debug with ads team
- Fix was reverted later, causing another outage
- Emphasizes: bind actions to user actions, not component lifecycle
State Management and Effects
- UI = function of state
- State + Event = Next State + Effects
- Effects happen in state transitions
- State machines provide declarative effect management
- Entry, exit, and transition actions
- Effects executed outside component rendering cycle
External Stores and Libraries
- useSyncExternalStore for React 18
- Libraries like Redux, XState use this pattern
- XState provides declarative effect management
- State machine controls effect execution
- Eliminates need for useEffect in components
Theo's Critique of XState
- Too many esoteric concepts
- Complexity often at lower level (connected state sub-states)
- Example: video call state management
- High level: connected/disconnected
- Low level: video enabled, audio enabled, user count, subscription states
- Prefers managing subscription state close to where rendered
- Unmounting component handles unsubscription automatically
Data Fetching Patterns
- Move away from "fetch on render" pattern
- React team prefers "render as you fetch"
- Use Suspense for data loading
- Frameworks like Remix/Next help eliminate useEffect for data
- React Query/SWR still valuable for async orchestration
- Criticism: many frameworks assume simple CRUD applications
Theo's Application Complexity Perspective
- Building video applications requires different patterns
- Most web frameworks designed for dashboards/e-commerce
- Majority of websites are static, but web apps require more developers
- React primitives allow community to build specialized solutions
- Speed/performance not replicable in some frameworks without effort
Code Examples and Patterns
- Ref flags for single execution effects
- React 18 virtual unmount/remount handling
- Suspense simplifies data fetching code
- State management libraries should handle effects declaratively
- Move from imperative to declarative effect management
Final Recommendations
- UseEffect for synchronization, not general effects
- State transitions trigger effects
- Effects go in event handlers when possible
- Render as you fetch with Suspense
- Model effects with state machines or similar patterns
- Abstract state management outside React components
