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

    Handling events

    onClick and onChange, passing a function rather than calling it, and the event object.

    7 minutes2 games

    To respond to the user, pass a function to an element through a prop like onClick, onChange or onSubmit. React calls it when the event happens and hands it an event object with the details.

    React
    function App() {
      function handleClick() {
        alert("Saved!")
      }
    
      return (
        <div>
          <button onClick={handleClick}>Save</button>
          <button onClick={() => console.log("clicked at", new Date().toLocaleTimeString())}>Log time</button>
          <input placeholder="Type here" onChange={(e) => console.log(e.target.value)} />
        </div>
      )
    }

    Important

    Pass the function itself: onClick={handleClick}. Writing onClick={handleClick()} calls it during render, not on click. To pass an argument, wrap it in an arrow function: onClick={() => remove(id)}.

    React
    function Toolbar({ onAction }) {
      return (
        <div style={{ display: "flex", gap: 8 }}>
          {["Copy", "Run", "Share"].map((label) => (
            <button key={label} onClick={() => onAction(label)}>{label}</button>
          ))}
        </div>
      )
    }
    
    function App() {
      return <Toolbar onAction={(name) => console.log("You pressed " + name)} />
    }

    The parent passes onAction as a prop

    A button in the child is clicked

    The child calls onAction("Run")

    The parent's code runs

    This lets a child "report" to its parent without knowing what it will do.

    e.preventDefault() stops the browser's default behaviour, like a form reloading the page on submit. e.stopPropagation() stops the event bubbling up to parent elements.

    Spot the bug

    The alert appears as soon as the page loads, not on click. Which line?

    Interview question

    What is event delegation, and how does React use it?

    Quick question

    Which is right for deleting the item with id on click?

    Learn more from

    • react.dev: Responding to eventsreact.dev
    • MDN: Event bubblingdeveloper.mozilla.org
    • React blog: React 17 (event delegation changes)legacy.reactjs.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousConditional renderingNextuseState: a component's memory
    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.