The Compiler's Mental Model: Reactive Dependencies
You'll learn: How React's compiler automatically tracks which values trigger re-computation, eliminating the need for manual dependency arrays.
The Dependency Detective
Remember manually managing useMemo and useCallback dependency arrays? The React compiler does this automatically by analyzing your code to understand which values are "reactive" — meaning they come from props, state, or other reactive sources.
How the Compiler Thinks
When the compiler reads your component, it builds a dependency graph:
function SearchResults({ query, filters }) {
// Compiler sees: results depends on query and filters
const results = expensiveFilter(data, query, filters);
// Compiler sees: onClick depends on results
const onClick = () => handleClick(results);
return <div>...</div>;
}
The compiler traces every variable to its source:
- Does it come from props? Reactive.
- Does it come from state (
useState,useReducer)? Reactive. - Is it computed from reactive values? Also reactive.
- Is it a constant or local primitive? Not reactive.
Automatic Memoization Decisions
Based on this analysis, the compiler automatically memoizes expressions that depend on reactive values. If query changes but filters doesn't, the compiler ensures only code dependent on query re-runs — without you writing a single dependency array.
Think of it like a smart assistant that watches what ingredients (dependencies) go into each dish (computation), then automatically preps only what needs refreshing when ingredients change.
Key Takeaway: The compiler analyzes your code's data flow to automatically determine what depends on what, inserting memoization only where reactive dependencies make it necessary — no manual dependency arrays required.