Effect Dependencies

An effect with [query] runs only when query changes. Type in the box, then change the theme, and compare.

React · beginner · 7 min · 26 steps · free

What you'll learn

EffectDeps.jsx

import { useState, useEffect } from 'react';

export default function SearchBox() {
  const [query, setQuery] = useState('');
  const [theme, setTheme] = useState('light');

  useEffect(() => {
    console.log(`searching for "${query}"`);
  }, [query]);

  return (
    <div className={theme}>
      <input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="Search" />
      <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>Theme: {theme}</button>
    </div>
  );
}

Step by step

  1. React renders <SearchBox> for the first time: it calls the SearchBox function.
  2. useState runs for the first time, so React stores the starting value for query and returns it: "".
  3. useState runs for the first time, so React stores the starting value for theme and returns it: "light".
  4. This effect lists [query]: after each render, React runs it only if query changed.
  5. SearchBox returns JSX: a description of what the screen should show, <div className="light">…</div>. React has not changed the page yet.
  6. React turns the JSX into real page elements and shows them: look at the Screen panel.
  7. After the first render, every effect runs once.
  8. Prints “searching for ""”.
  9. You type “react” into the input. React calls its onChange handler.
  10. setQuery(e.target.value) does not change query right away: React queues the update and will render again. In this call query is still "".
  11. React renders <SearchBox> again (render 2): it calls SearchBox once more, from the top.
  12. useState returns the value React has kept for query: "react". The starting value is only used on the first render.
  13. useState returns the value React has kept for theme: "light". The starting value is only used on the first render.
  14. useEffect does not run the effect now. React runs it after the page has been updated, and again only when a dependency changes.
  15. SearchBox returns JSX: a description of what the screen should show, <div className="light">…</div>. React has not changed the page yet.
  16. React compares the new JSX with the last one and updates only what changed on the page.
  17. query changed from "" to "react", so React runs the effect again.
  18. Prints “searching for "react"”.
  19. You click the theme button. Only theme will change.
  20. setTheme(theme === 'light' ? 'dark' : 'light') does not change theme right away: React queues the update and will render again. In this call theme is still "light".
  21. React renders <SearchBox> again (render 3): it calls SearchBox once more, from the top.
  22. useState returns the value React has kept for query: "react". The starting value is only used on the first render.
  23. useState returns the value React has kept for theme: "dark". The starting value is only used on the first render.
  24. The effect is declared again with [query] = ["react"].
  25. SearchBox returns JSX: a description of what the screen should show, <div className="dark">…</div>. React has not changed the page yet.
  26. The page updates, but the effect does not run: its only dependency, query, did not change. The Output shows no new search.

Common mistakes

Best practices

Key takeaways

Check yourself

useEffect(fn, []) runs?

Answer: Once, after the first render

How does React know a dependency changed?

Answer: It compares with Object.is

·

More in Effects and Refs