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

    State and events · Lesson 7 of 21

    useState: a component's memory

    Why a plain variable doesn't work, re-rendering, and updater functions.

    10 minutes2 games

    Try a counter with a plain variable and the number on the page never changes. Why? 1) A local variable resets every time the function runs, and 2) changing it doesn't tell React to redraw.

    useState fixes both: it gives you a value React remembers between renders, and a set function that makes React re-render the component with the new value.

    React
    function App() {
      let plain = 0
      const [count, setCount] = useState(0)
    
      return (
        <div>
          <p>plain: {plain} · state: {count}</p>
          <button onClick={() => { plain++; console.log("plain is", plain) }}>plain++</button>
          <button onClick={() => setCount(count + 1)}>state + 1</button>
        </div>
      )
    }
    Step by step1 / 5
    1. ▶const [count, setCount] = useState(0)
    2. 2// click: setCount(count + 1)
    3. 3// React re-runs App()
    4. 4const [count, setCount] = useState(0)
    5. 5return <p>{count}</p>

    Variables

    count0
    render1

    Output

    …

    First render: count = 0.

    useState's 0 is used only the first time. After that React returns the value it remembers.

    Updater functions

    Within one render, count is fixed, like a snapshot. Call setCount(count + 1) three times in a row and all three see the same count: the result is +1. When the new value depends on the old one, pass a function: setCount(c => c + 1).

    React
    function App() {
      const [a, setA] = useState(0)
      const [b, setB] = useState(0)
    
      function plusThreeWrong() {
        setA(a + 1)
        setA(a + 1)
        setA(a + 1)
      }
      function plusThreeRight() {
        setB((n) => n + 1)
        setB((n) => n + 1)
        setB((n) => n + 1)
      }
    
      return (
        <div>
          <p>a = {a} <button onClick={plusThreeWrong}>+3 (wrong)</button></p>
          <p>b = {b} <button onClick={plusThreeRight}>+3 (right)</button></p>
        </div>
      )
    }

    Important

    Rules of hooks: call useState (and every hook) only at the top level of a component, never inside an if, a loop or a nested function. React identifies each piece of state by call order; if the order changes between renders, values get mixed up.

    What's the output?

    function App() {
      const [n, setN] = useState(5)
      useEffect(() => {
        setN(n + 1)
        setN(n + 1)
      }, [])
      return <p>{n}</p>
    }

    What will the page show?

    Interview question

    Why doesn't setState change the value immediately? What is batching?

    Exercise

    Build an XP counter: a +10 button adds 10, a Reset button sets it to zero, and the text reads XP: N. The checker presses +10 three times.

    The page should show:
    XP: 30
    +10 Reset
    React
    function App() {
      return (
        <div>
          <p>XP: 0</p>
          <button>+10</button>
          <button>Reset</button>
        </div>
      )
    }

    Learn more from

    • react.dev: State, a component's memoryreact.dev
    • react.dev: State as a snapshotreact.dev
    • react.dev: Queueing a series of state updatesreact.dev
    • react.dev: Rules of Hooksreact.dev
    • Josh W. Comeau: Why React re-rendersjoshwcomeau.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousHandling eventsNextObjects and arrays in state
    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.