🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

onClick

AI & DATA SCIENCE // onclick

onClick is the React event prop for handling mouse click events on an element, receiving a SyntheticEvent object as its argument when triggered.

Syntax

<button onClick={handleClick}>Click me</button>

Deep Dive Course

onClick fires whenever the element it's attached to, or one of its descendants since it bubbles, is clicked, calling the provided handler function with a SyntheticMouseEvent object containing details like which mouse button was pressed, the click's coordinates, and standard methods like preventDefault() and stopPropagation(). It works on virtually any element, not just buttons and links, letting you make arbitrary elements, like a div or a table row, clickable, though for accessibility, genuinely interactive elements should generally be actual buttons or links rather than a div with an onClick handler and no other semantic or keyboard support.

1Understanding onClick

onClick fires whenever the element it's attached to, or one of its descendants since it bubbles, is clicked, calling the provided handler function with a SyntheticMouseEvent object containing details like which mouse button was pressed, the click's coordinates, and standard methods like preventDefault() and stopPropagation(). It works on virtually any element, not just buttons and links, letting you make arbitrary elements, like a div or a table row, clickable, though for accessibility, genuinely interactive elements should generally be actual buttons or links rather than a div with an onClick handler and no other semantic or keyboard support.

💡

Avoid making a plain div the sole clickable target for an important action — use an actual button, or add proper keyboard and ARIA support, so the element remains accessible to keyboard and screen-reader users, not just mouse users.

editor.html
function LikeButton() {
  const handleClick = () => console.log('Liked!');
  return <button onClick={handleClick}>Like</button>;
}
localhost:3000

2Practical Example

Here is a real-world application of onClick showing how it is used in production React code.

editor.html
function Card({ onSelect, id }) {
  return (
    <div onClick={() => onSelect(id)}>
      <button onClick={(e) => { e.stopPropagation(); console.log('Delete clicked'); }}>Delete</button>
    </div>
  );
}
localhost:3000

3Best Practices

Follow these guidelines when working with onClick:

1. Prefer semantic elements like <button> for clickable actions over a <div> with onClick, for built-in keyboard accessibility and screen-reader support

2. Use event.stopPropagation() inside a nested element's onClick handler if you need to prevent the click from also triggering a parent element's own onClick due to bubbling

3. Pass a function reference, or an inline arrow function when arguments are needed, never a direct function call, to onClick

⚠️

Tip: Avoid making a plain div the sole clickable target for an important action — use an actual button, or add proper keyboard and ARIA support, so the element remains accessible to keyboard and screen-reader users, not just mouse users.

editor.html
function LikeButton() {
  const handleClick = () => console.log('Liked!');
  return <button onClick={handleClick}>Like</button>;
}
localhost:3000

Examples

Example 01Basic Usage
function LikeButton() {
  const handleClick = () => console.log('Liked!');
  return <button onClick={handleClick}>Like</button>;
}
Example 02Advanced Example
function Card({ onSelect, id }) {
  return (
    <div onClick={() => onSelect(id)}>
      <button onClick={(e) => { e.stopPropagation(); console.log('Delete clicked'); }}>Delete</button>
    </div>
  );
}

Best Practices

  • Prefer semantic elements like
  • Use event.stopPropagation() inside a nested element's onClick handler if you need to prevent the click from also triggering a parent element's own onClick due to bubbling
  • Pass a function reference, or an inline arrow function when arguments are needed, never a direct function call, to onClick

Interview Question

Why does clicking a nested button inside a div that also has its own onClick handler trigger both handlers by default, and how would you prevent that?

Hint: Think about event bubbling — does a click event fire only on the exact element clicked, or does it also propagate upward through ancestor elements?

A click event doesn't just fire on the exact element that was clicked, it also bubbles upward through that element's ancestors in the DOM tree, triggering any click handlers attached to those ancestors too, unless something explicitly stops that propagation partway through. This means clicking a button nested inside a div, where both have their own onClick handlers, fires the button's handler first, and then the event continues bubbling up and also fires the div's handler, since by default nothing stops it from doing so. Calling event.stopPropagation() inside the inner button's handler explicitly halts that bubbling at that point, preventing the event from continuing upward and therefore preventing the outer div's handler from firing for that particular click.

Exercises

MediumPractice using onClick in a real scenario.
View Solution
function LikeButton() {
  const handleClick = () => console.log('Liked!');
  return <button onClick={handleClick}>Like</button>;
}

Frequently Asked Questions

Why does clicking a nested button inside a div that also has its own onClick handler trigger both handlers by default, and how would you prevent that?

A click event doesn't just fire on the exact element that was clicked, it also bubbles upward through that element's ancestors in the DOM tree, triggering any click handlers attached to those ancestors too, unless something explicitly stops that propagation partway through. This means clicking a button nested inside a div, where both have their own onClick handlers, fires the button's handler first, and then the event continues bubbling up and also fires the div's handler, since by default nothing stops it from doing so. Calling event.stopPropagation() inside the inner button's handler explicitly halts that bubbling at that point, preventing the event from continuing upward and therefore preventing the outer div's handler from firing for that particular click.

Related Functions

events-in-reactonsubmitsynthetic-events