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

    Lifting state up

    When two components need the same data: a single source of truth.

    9 minutes2 games

    If two sibling components need the same state (like a course list and a cart counter), don't give each its own copy; they'll drift apart. Lift the state to their closest common parent, and pass the value down as props along with a function to change it.

    React
    const COURSES = [
      { id: 2, title: "JavaScript", price: 450 },
      { id: 3, title: "Python", price: 400 },
      { id: 5, title: "React", price: 600 },
    ]
    
    function CourseList({ cart, onToggle }) {
      return (
        <ul>
          {COURSES.map((c) => (
            <li key={c.id}>
              {c.title} ({c.price}){" "}
              <button onClick={() => onToggle(c.id)}>{cart.includes(c.id) ? "Remove" : "Add"}</button>
            </li>
          ))}
        </ul>
      )
    }
    
    function CartSummary({ cart }) {
      const total = COURSES.filter((c) => cart.includes(c.id)).reduce((sum, c) => sum + c.price, 0)
      return <p>🛒 {cart.length} courses · {total} EGP</p>
    }
    
    function App() {
      const [cart, setCart] = useState([])
      const toggle = (id) => setCart((c) => (c.includes(id) ? c.filter((x) => x !== id) : [...c, id]))
      return (
        <>
          <CartSummary cart={cart} />
          <CourseList cart={cart} onToggle={toggle} />
        </>
      )
    }
    • App: cart state + toggle()
      • CartSummary ← cart (props down)
      • CourseList ← cart, onToggle
        • button → onToggle(id) (event up)

    Data flows down as props; events flow up as callbacks.

    Important

    Don't store in state what you can compute: total above isn't state; it's derived from cart on every render. As state you'd have to update it on every change, and forgetting once leaves it wrong.

    Order the lines

    Order the code: the parent owns the state and passes it down with a setter.

    1. Click the lines in the right order

    Interview question

    When do you lift state, and when do you reach for Context or a state library?

    Exercise

    A Stepper component shows only - and + buttons, while the parent App holds the number and shows Seats: N (starting at 1). The checker presses + twice and - once.

    The page should show:
    Seats: 2
    - +
    React
    function Stepper({ onChange }) {
      return (
        <div>
          <button className="minus">-</button>
          <button className="plus">+</button>
        </div>
      )
    }
    
    function App() {
      return (
        <div>
          <p>Seats: 1</p>
          <Stepper />
        </div>
      )
    }

    Learn more from

    • react.dev: Sharing state between componentsreact.dev
    • react.dev: Choosing the state structurereact.dev
    • react.dev: Thinking in Reactreact.dev
    • Kent C. Dodds: Application state management with Reactkentcdodds.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousForms and controlled inputsNextuseEffect
    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.