Skip to content
Core ConceptsChapter 9 / 37Beginner4 min read

Handling Events

Learn how to write React onClick and other event handlers, pass arguments, use preventDefault and work with the event object.

On this page

1. What is handling events?

In React, events are attached through JSX attributes such as onClick, onChange and onSubmit. The names are in camelCase and the value is a function, not a string.

2. Why does it matter?

An interactive UI means something happens when the user acts. Without events, a React app is just a static page.

Plain DOMReact events
Find the element with querySelectorWrite the handler right in JSX
Attach addEventListener manuallyThe onClick attribute is enough
Remember to remove the listenerReact manages it for you
Logic and markup live in different placesThe handler sits inside the component, next to the state

3. Where is it used?

EventWhen it firesExample
onClickA click or tapLike button, menu toggle
onChangeThe input value changesSearch box, checkbox
onSubmitA form is submittedLogin form
onKeyDownA key is pressedSend a message on Enter
onMouseEnterThe mouse moves over the elementShowing a tooltip

4. How does it work?

From click to UI update
The user clicks a button
React calls your handler and passes an event object
The handler updates state
React re-renders
The new UI appears on the screen

You pass a reference to the function (onClick={handleClick}), and React calls it at the right time. If you write handleClick(), it runs during render instead.

5. Code example

Basic click handler

jsx
import { useState } from "react";

export default function LikeButton() {
  const [likes, setLikes] = useState(0);

  function handleClick() {
    setLikes(likes + 1);
  }

  return <button onClick={handleClick}>Like ({likes})</button>;
}

Passing an argument

jsx
function SizePicker({ onSelect }) {
  const sizes = ["S", "M", "L"];

  return (
    <div>
      {sizes.map((size) => (
        <button key={size} onClick={() => onSelect(size)}>
          {size}
        </button>
      ))}
    </div>
  );
}

The event object and preventDefault

jsx
function SearchBox() {
  function handleKeyDown(e) {
    if (e.key === "Enter") {
      e.preventDefault();
      console.log("Search:", e.currentTarget.value);
    }
  }

  return <input onKeyDown={handleKeyDown} placeholder="Search..." />;
}

How does the code work?

PartMeaning
onClick={handleClick}A reference to the function, which React calls on click
() => onSelect(size)An arrow function so the argument is passed later, at click time
eThe event object (React's synthetic event, with a browser-like API)
e.keyWhich key was pressed
e.preventDefault()Stops the browser's default behavior
onSelect propA parent function passed to the child, which the child calls on an event

Handler props are usually named with on (onSelect), and handler functions with handle (handleClick).

6. Common mistakes

Mistake: Calling the function in onClick instead of passing a reference

Why? handleClick() runs during render. If it sets state, it can cause an infinite re-render loop.

Better: Write onClick={handleClick}, or onClick={() => handleClick(id)} if you need an argument.

Mistake: Writing lowercase onclick with a string value like in HTML

Why? React expects a camelCase name and a function value. A string handler does not work.

Better: Write onClick, onChange and onSubmit in camelCase and pass a function.

Mistake: Not understanding why a click on a nested element also runs the parent's handler

Why? Events bubble, so a click on the child also reaches the parent.

Better: If needed, call e.stopPropagation() in the child handler.

Interview tip

You may be asked "What is event bubbling?" Answer: the event first fires on the target element, then travels up through its parents. stopPropagation stops that, while preventDefault only stops the browser's default action.

7. What to learn before and after

Final quick revision

PointDetail
NamingcamelCase: onClick, onChange
ValueA function reference, not a call
ArgumentsWrap in an arrow function
Event objectThe first parameter of the handler
Stop default behaviore.preventDefault()