Ojasa Mirai

Ojasa Mirai

ReactJS

Loading...

Learning Level

🟢 Beginner🔵 Advanced
🎪 Event Handling Basics🖱️ Click Events⌨️ Keyboard Events📝 Form Events🎯 Event Handlers with Parameters🚫 Event Default Behavior🔗 Event Delegation⚡ Performance & Events
Reactjs/Events/Click Events

🖱️ Click Events

Click events are the foundation of user interaction. In React, `onClick` is the primary event handler for clicks.

Basic Click Handler

function Counter() {
  const [count, setCount] = React.useState(0);

  const handleClick = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Increment</button>
    </div>
  );
}

Passing Parameters to Click Handlers

function ButtonGroup() {
  const handleClick = (id) => {
    console.log("Button " + id + " clicked");
  };

  return (
    <div>
      <button onClick={() => handleClick(1)}>Button 1</button>
      <button onClick={() => handleClick(2)}>Button 2</button>
    </div>
  );
}

Click Event Object

The click event gives you access to mouse information:

function MouseInfo() {
  const handleClick = (e) => {
    console.log("X:", e.clientX, "Y:", e.clientY);
    console.log("Button:", e.button); // 0=left, 1=middle, 2=right
  };

  return <button onClick={handleClick}>Get Mouse Position</button>;
}

Multi-Click Patterns

<details>

<summary>Double Click</summary>

\`\`\`jsx

<button onDoubleClick={handleDoubleClick}>

Double Click Me

</button>

\`\`\`

</details>

<details>

<summary>Right Click (Context Menu)</summary>

\`\`\`jsx

const handleRightClick = (e) => {

e.preventDefault();

console.log("Right clicked");

};

<button onContextMenu={handleRightClick}>

Right Click Me

</button>

\`\`\`

</details>

✅ Key Takeaways

  • Use `onClick` with **arrow functions** to pass parameters
  • Access click data via the **event object** (clientX, clientY, button)
  • **Double-click** uses `onDoubleClick`
  • **Right-click** uses `onContextMenu` with `preventDefault()`

Resources

Python Docs

Ojasa Mirai

Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.

Learn Deep • Build Real • Verify Skills • Launch Forward

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn