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 8 of 21

    Objects and arrays in state

    Don't mutate, make a new copy: spread, map and filter.

    10 minutes2 games

    React compares old and new state with Object.is: "is this the same object?" If you change the object itself (user.name = …) and set it again, React sees the same object and skips the re-render. The rule: make a new copy with the change.

    React
    function App() {
      const [profile, setProfile] = useState({ name: "Karim", city: "Alexandria", xp: 150 })
      const [tags, setTags] = useState(["web", "data"])
    
      return (
        <div>
          <p>{profile.name} · {profile.city} · {profile.xp} xp</p>
          <button onClick={() => setProfile({ ...profile, xp: profile.xp + 50 })}>+50 xp</button>
          <button onClick={() => setProfile({ ...profile, city: "Cairo" })}>Move to Cairo</button>
    
          <p>{tags.join(", ")}</p>
          <button onClick={() => setTags([...tags, "ai"])}>Add tag</button>
          <button onClick={() => setTags(tags.filter((t) => t !== "web"))}>Remove web</button>
          <button onClick={() => setTags(tags.map((t) => t.toUpperCase()))}>Uppercase</button>
        </div>
      )
    }
    To❌ mutates✅ new copy

    add

    arr.push(x)

    [...arr, x]

    remove

    arr.splice(i, 1)

    arr.filter((_, j) => j !== i)

    replace

    arr[i] = x

    arr.map((v, j) => j === i ? x : v)

    sort

    arr.sort()

    [...arr].sort()

    change a field

    obj.x = 1

    { ...obj, x: 1 }

    Important

    Spread { ...obj } makes a shallow copy: nested objects are still shared. With { user: { address: { city } } }, changing city means copying each level: { ...s, user: { ...s.user, address: { ...s.user.address, city } } }. If that gets heavy, flatten the state, or use a library like Immer.

    Spot the bug

    The button adds the task (see the console) but the screen doesn't update. Which line?

    Interview question

    Why does immutability matter in React?

    Exercise

    A task list: each task has a done button that removes it, and the first line shows N left. The checker presses the first button twice.

    The page should show:
    1 left
    Quiz done
    React
    function App() {
      const [tasks, setTasks] = useState(["Read", "Practice", "Quiz"])
      return (
        <div>
          <p>{tasks.length} left</p>
          <ul>
            {tasks.map((t) => (
              <li key={t}>{t} <button>done</button></li>
            ))}
          </ul>
        </div>
      )
    }

    Learn more from

    • react.dev: Updating objects in statereact.dev
    • react.dev: Updating arrays in statereact.dev
    • MDN: Spread syntaxdeveloper.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PrevioususeState: a component's memoryNextForms and controlled inputs
    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.