# Event Handling

> How to respond to user interactions like clicks, inputs, and form submissions.

Subject: React | Lesson 10 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/event-handling
Previous: [Functional State Updates](https://codewithanimation.in/react/functional-state-updates.md) | Next: [Conditional Rendering](https://codewithanimation.in/react/conditional-rendering.md)

## What it is

React allows you to respond to user interactions by attaching **event handlers** directly to JSX elements.

Instead of lowercase HTML attributes like `onclick`, React uses camelCase props like `onClick`, `onChange`, and `onSubmit`.

## Why it matters

Without event handlers, your UI is completely static. Event handlers are the bridge between the user's actions and your component's logic.

React normalizes these events across all browsers, so an `onClick` behaves exactly the same in Chrome, Firefox, and Safari without you having to write browser-specific workarounds.

## How to use it

You define a function inside your component, usually starting with the word `handle` (e.g., `handleClick` or `handleSubmit`).

You then pass that function as a prop to a JSX element: `<button onClick={handleClick}>`.

## Common mistake: The instant-execution trap

The most common beginner mistake is adding parentheses to the function: `<button onClick={handleClick()}>`.

Because JavaScript executes functions immediately when it sees parentheses, this will fire the click handler *during the render*, not when the user actually clicks the button!

You must pass the function **reference**: `<button onClick={handleClick}>` or use an inline arrow function: `<button onClick={() => handleClick()}>`.

## Tip: Preventing Default

Just like standard JavaScript, React event handlers receive an event object (`e`). If you are handling a `<form onSubmit={handleSubmit}>`, remember to call `e.preventDefault()` inside your handler to stop the browser from refreshing the page.

## Try it: The Click Trap

Test the buttons below. The first button passes a function call (with parentheses). The second button passes a function reference.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/event-handling

## Check yourself

### Question 1

What is the correct syntax for adding a click handler to a button in React?

- A. <button onclick="handleClick()">
- B. <button onClick={handleClick}>
- C. <button onClick={handleClick()}>

Correct answer: B

React requires camelCase props and a JavaScript function reference passed inside curly braces.

- Why not A: This is standard HTML syntax, not React.
- Why not C: This will execute the function immediately during the render.

### Question 2

Why is `<button onClick={doSomething()}>` a bug?

- A. Because React does not support the `doSomething` function name.
- B. Because the function will execute immediately every time the component renders, instead of waiting for the click.
- C. Because it is missing the `event` argument.

Correct answer: B

Parentheses in JavaScript mean 'execute this right now'. You must pass the function reference without parentheses so React can call it later.

- Why not A: The function name doesn't matter.
- Why not C: You don't need to pass the event argument if you aren't using it.

### Question 3

How do you stop a form submission from refreshing the entire page in React?

- A. Call `e.preventDefault()` inside your `onSubmit` handler.
- B. Add `noRefresh={true}` to the `<form>` tag.
- C. React automatically prevents default on all forms.

Correct answer: A

React wraps standard DOM events in SyntheticEvents, but methods like `preventDefault()` and `stopPropagation()` work exactly the same as in vanilla JS.

- Why not B: This prop does not exist.
- Why not C: React does not automatically prevent default browser behaviors.

## Remember this

- Event names are camelCase in React (`onClick`, `onChange`).
- Pass a function reference, not a function call (no parentheses).
- If you need to pass arguments, wrap the call in an arrow function: `onClick={() => deleteItem(id)}`.
- Call `e.preventDefault()` for forms and links that shouldn't trigger browser navigations.
