Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    Effects and essential hooks · Lesson 11 of 21

    useEffect

    Syncing with the outside world, dependencies, and cleanup.

    11 minutes2 games

    Rendering must be a pure calculation: props and state in, JSX out. Anything else, like starting a timer, calling an API, changing the page title or subscribing to an event, is a side effect, and it belongs in useEffect. React runs effects after the screen updates.

    React
    function Clock() {
      const [now, setNow] = useState(new Date())
    
      useEffect(() => {
        const id = setInterval(() => setNow(new Date()), 1000)
        console.log("timer started")
        return () => {
          clearInterval(id)               // cleanup
          console.log("timer stopped")
        }
      }, [])                              // [] = run once, after the first render
    
      return <p>{now.toLocaleTimeString()}</p>
    }
    
    function App() {
      const [show, setShow] = useState(true)
      return (
        <div>
          <button onClick={() => setShow(!show)}>{show ? "Hide" : "Show"} clock</button>
          {show && <Clock />}
        </div>
      )
    }
    DependenciesWhen the effect runs

    no array

    after every render

    []

    once, after the first render

    [userId]

    first time, and whenever userId changes

    The function an effect returns is its cleanup. React calls it before running the effect again, and when the component leaves the page. Press "Hide clock" and watch "timer stopped" in the console. Without cleanup the timer would run forever (a memory leak).

    React
    function App() {
      const [title, setTitle] = useState("SeaCode")
    
      useEffect(() => {
        console.log("syncing title:", title)
        document.title = title
      }, [title])                          // re-run only when title changes
    
      return <input value={title} onChange={(e) => setTitle(e.target.value)} />
    }

    Important

    Every prop or state value used inside the effect must be in its dependencies. Forget one and the effect keeps seeing the old value (a stale closure). Many things need no effect at all: compute derived values during render, and respond to clicks in the event handler.

    Spot the bug

    The counter stops at 1 and never goes further. Which line is the cause?

    Interview question

    Why does an effect run twice in development?

    Exercise

    Build a countdown from 3: every 100 ms the number drops by one, and at zero it shows Go! and the interval stops. (The page starts by showing 3.)

    The page should show: Go!

    React
    function App() {
      const [n, setN] = useState(3)
    
      return <h2>{n}</h2>
    }

    Learn more from

    • react.dev: Synchronizing with effectsreact.dev
    • react.dev: You might not need an effectreact.dev
    • react.dev: useEffect referencereact.dev
    • Overreacted: A complete guide to useEffectoverreacted.io

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousLifting state upNextFetching data
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.