Stop 08 · React FundamentalsTopics · 6/10

Event Handling in React

Part of the MERN Stack Roadmap

In short

A page that cannot react to clicks and typing is just a poster. Event handlers are how your React app listens to the user and decides what to do next.

React attaches handlers with props like onClick, onChange and onSubmit. You pass a function, not a function call, and use event.preventDefault() to stop default form behaviour.

The jaadu stands behind a hotel reception desk, hand hovering over a shiny brass bell, ready to spring into action the moment it dings.

Think of it like this

The bell on the hotel desk

A hotel desk has a bell, and the manager tells the receptionist: "When this bell rings, you go and help." You hand over the instruction, you don't ring the bell yourself right now. In React, onClick={handleClick} is that instruction. Writing onClick={handleClick()} is like ringing the bell the moment you hire the receptionist.

Watch it happen

What happens when the user clicks Add to cart

User
Button
handleAdd
React

click

1/4Priya clicks the Add to cart button on a ₹499 T-shirt.

The big ideas

What you need to know

Pass a function, not a call

onClick={handleClick} hands React a function to call later. onClick={handleClick()} runs it during render, which is almost always a bug.

Passing arguments

Wrap it in an arrow function: onClick={() => removeItem(item._id)}. The arrow runs only when the click happens.

preventDefault on forms

A form submit reloads the page by default. Call event.preventDefault() in onSubmit so React stays in control.

camelCase names

React uses onClick, onChange, onSubmit, onKeyDown. Lowercase onclick strings from plain HTML do not work in JSX.

See it in code

Walk through the code

jsx
1import { useState } from "react";2 3export default function NewsletterForm() {4  const [email, setEmail] = useState("");5  const [message, setMessage] = useState("");6 7  function handleSubmit(event) {8    event.preventDefault();9    if (!email.includes("@")) {10      setMessage("Please enter a valid email.");11      return;12    }13    setMessage("Thanks! You are on the list.");14    setEmail("");15  }16 17  return (18    <form onSubmit={handleSubmit}>19      <input20        type="email"21        value={email}22        onChange={(e) => setEmail(e.target.value)}23        placeholder="you@example.com"24      />25      <button type="submit">Subscribe</button>26      <button type="button" onClick={() => setEmail("")}>Clear</button>27      {message && <p>{message}</p>}28    </form>29  );30}

1/5The submit handler receives the event and stops the browser from reloading the page.

A small form with three handlers: submit, change and a clear button.

Save yourself the pain

Common mistakes

Oops

Writing onClick={deleteNote(note._id)} to pass an id.

Do this instead

Write onClick={() => deleteNote(note._id)}. Otherwise deleteNote runs on every render and the click does nothing.

Oops

Forgetting event.preventDefault() in a form submit handler.

Do this instead

Call it first thing in onSubmit, or the page reloads and your state is lost.

Oops

Leaving extra buttons inside a form without a type.

Do this instead

Buttons inside a form default to type="submit". Give non-submit buttons type="button".

Your mission

A like button and a search box

~20 min

Quick check

Which line correctly deletes the note with this id only when the button is clicked?

Go deeper

  1. 1Add click, change and submit handlers to a small form.
  2. 2Pass arguments to a handler with an arrow function.
  3. 3Call preventDefault in onSubmit so the page does not reload.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge