Course contentsShow
← React
Lesson 881 of 2,39623. useMemo and useCallbackFree lesson

When Empty Dependencies Are Valid

Rare cases where useMemo/useCallback with empty arrays are correct and when they indicate bugs.

When Empty Dependencies Are Valid

You'll learn when an empty dependency array in useMemo/useCallback is intentionally correct versus a red flag for bugs.

The Two Faces of Empty Arrays

An empty dependency array [] tells React "never recompute this value after the first render." Sometimes this is exactly what you want. Other times, it's a ticking time bomb.

Valid Use Case: Truly Static Values

Empty dependencies are correct when the memoized value genuinely never needs to change:

const initialConfig = useMemo(() => {
  return {
    apiUrl: 'https://api.example.com',
    timeout: 5000,
    retries: 3
  };
}, []); // Valid: configuration never changes

This works because the object contains only hardcoded values. Nothing from props, state, or context influences it.

The Bug Pattern: Stale Closures

Empty arrays become bugs when you forget dependencies that should be there:

function SearchBox({ onSearch }) {
  const [query, setQuery] = useState('');
  
  const handleSubmit = useCallback(() => {
    onSearch(query); // Bug! Uses `query` but not in deps
  }, []); // Wrong: should include [query, onSearch]
  
  // handleSubmit always submits the initial empty string
}

The callback "closes over" the first render's query value and never updates.

The Litmus Test

Ask yourself: "Does this computation read any value that could change between renders?" If yes, those values belong in the dependency array. If no—truly no props, state, or reactive values—then [] is valid.

Think of empty dependencies like a time capsule: safe for things that belong in the past, dangerous for things that need to stay current.

Key Takeaway: Empty dependency arrays are valid only when the memoized value depends on nothing reactive—no props, state, or context. Otherwise, you're creating a stale closure bug.