Function Dependencies That Recreate Every Render
You'll learn why including regular functions in your dependency array creates infinite loops—and how to recognize this trap.
The Problem: Functions Are New Every Render
Remember how object and array dependencies cause loops because they're recreated each render? Functions have the exact same problem.
When you define a function inside your component, JavaScript creates a brand new function on every render—even if the code is identical. This means if you include that function in your dependency array, React sees it as "changed" every time, triggering your effect... which causes a re-render... which creates a new function... infinite loop.
function SearchBox() {
const [query, setQuery] = useState('');
const [results, setResults] = useState([]);
// This function is NEW every render
const fetchResults = (searchTerm) => {
fetch(`/api/search?q=${searchTerm}`)
.then(res => res.json())
.then(setResults);
};
useEffect(() => {
fetchResults(query);
}, [query, fetchResults]); // 💥 INFINITE LOOP
// ...
}
Every render creates a new fetchResults. The effect sees a "new" dependency and runs. Running the effect may trigger state updates, causing another render, creating another new fetchResults, and so on forever.
The Analogy
It's like having a security guard who checks if anyone at the door is "new." You keep sending the same person, but they're wearing a different hat each time, so the guard thinks it's a new visitor every single time.
Key Takeaway: Regular functions defined in your component body are recreated on every render. Including them in dependency arrays without useCallback creates infinite loops because React sees them as "new" dependencies each time.