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

    Components and JSX · Lesson 4 of 21

    Rendering lists and keys

    map and filter inside JSX, why keys matter, and why the index is usually wrong.

    9 minutes2 games

    To render an array, turn each item into JSX with map. Each item needs a `key` that's unique among its siblings, so React can tell them apart when the list changes.

    React
    const courses = [
      { id: 1, title: "HTML & CSS", price: 0 },
      { id: 2, title: "JavaScript", price: 450 },
      { id: 5, title: "React", price: 600 },
      { id: 6, title: "C++", price: 500 },
    ]
    
    function App() {
      const paid = courses.filter((c) => c.price > 0)
      return (
        <div>
          <h3>{paid.length} paid courses</h3>
          <ul>
            {paid.map((c) => (
              <li key={c.id}>{c.title}: {c.price} EGP</li>
            ))}
          </ul>
        </div>
      )
    }

    Why keys?

    Try this: type something into the input next to "Laila", then press the button that adds a student at the top. The left list uses the index as key, so your text jumps to another student. The right uses id, so it stays put.

    React
    function Row({ name }) {
      return <li>{name} <input placeholder="note" size={8} /></li>
    }
    
    function App() {
      const [people, setPeople] = useState([{ id: 1, name: "Laila" }, { id: 2, name: "Omar" }])
      const addFirst = () => setPeople([{ id: Date.now(), name: "New" }, ...people])
      return (
        <div>
          <button onClick={addFirst}>Add at the top</button>
          <div style={{ display: "flex", gap: 24 }}>
            <ul><b>key = index</b>{people.map((p, i) => <Row key={i} name={p.name} />)}</ul>
            <ul><b>key = id</b>{people.map((p) => <Row key={p.id} name={p.name} />)}</ul>
          </div>
        </div>
      )
    }
    BeforeAfter adding at the topkey = index

    0: Laila

    0: New

    React thinks 0 is still the same item and keeps its input

    1: Omar

    1: Laila

    and so on

    2: Omar

    a new item

    Important

    A key must be stable and unique among siblings: usually a database id. The index is only fine if the list never reorders, grows or shrinks. Never generate a random key each render (Math.random()): React would treat every item as new every time.

    Spot the bug

    React logs a warning here. Which line?

    Interview question

    Why does React need keys, and what goes wrong with the index?

    Exercise

    In a <ul>, list only the students with 200 xp or more, highest first, as Nour (410), each li with a key.

    The page should show:
    Nour (410)
    Laila (320)
    Mariam (275)
    React
    const students = [
      { id: 1, name: "Laila", xp: 320 },
      { id: 2, name: "Karim", xp: 150 },
      { id: 4, name: "Nour", xp: 410 },
      { id: 6, name: "Mariam", xp: 275 },
    ]
    
    function App() {
      return <ul></ul>
    }

    Learn more from

    • react.dev: Rendering listsreact.dev
    • react.dev: Preserving and resetting statereact.dev
    • MDN: Array.prototype.map()developer.mozilla.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousPropsNextConditional rendering
    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.