Reacting to the User: Core Events

Discover the fundamental events that make your React apps interactive. Learn to use `onClick`, `onChange`, and `onSubmit` to build dynamic user experiences.

Lesson ProgressStep 1 of 10

State: ""

0 EXP

Hello! Let's learn how to make React respond to users. We'll start with the most common event: `onClick`.

// Welcome to the React Events simulator

Handling Clicks: `onClick`

The `onClick` event is the most common way to make elements interactive. You pass a **function** (an event handler) to the `onClick` prop. This function will be executed every time the user clicks the element.

function handleClick() {
  console.log("Button was clicked!");
}

// Pass the function definition, do NOT call it
<button onClick={handleClick}>Click Me</button>

A common mistake is writing `onClick={handleClick()}`. This will **call the function immediately** when the component renders, not when it's clicked.

System Check

What do you pass to the `onClick` prop?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Event Handler

Correctly wire up an `onClick` event to a button.

🏗️
Form Master

Build a functional controlled component with `onChange`.

✍️
Event Pro

Prove your mastery by correctly using `e.preventDefault()`.

Mission: Build a Controlled Component

Edit the code to create a **controlled input**. The text you type in the input box should appear in the `<p>` tag in real-time.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Match the Event

Drag the event handlers on the right to match their correct element on the left.

<button>
<input>
<form>
onChange
onClick
onSubmit

Challenge: Complete the Form Handler

Fill in the missing parts to create a working form submission handler.

function handleSubmit() {;alert("Submitted!");}
return <form={handleSubmit}>...</form>

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Controlled Component" project for feedback from other Net-Runners.

Reacting to Users: The Core of Interactivity

A React application without events is just a static page. Events are the bridge that connects your user's actions—clicks, key presses, form submissions—to your component's logic and state. Mastering event handling is the single most important step toward building dynamic, interactive web applications.

In React, handling events is very similar to handling events on DOM elements, but with a few key differences:

  • **camelCase:** React event names are camelCased (onClick) instead of lowercase (onclick).
  • **Functions, Not Strings:** You pass a function as the event handler (onClick={handleClick}) rather than a string (onclick="handleClick()").

1. The Click Event: `onClick`

The most basic event. You can add it to almost any JSX element, but it's most commonly used on buttons.

function MyButton() {
  function handleClick() {
    alert('You clicked me!');
  }

  return (
    <button onClick={handleClick}>
      Click Me
    </button>
  );
}

Notice we pass `handleClick`, **not** `handleClick()`. If you include the parentheses, the function will execute **when the component renders**, not when the button is clicked. This is a very common mistake.

2. The Change Event: `onChange` and Controlled Components

This is the most critical concept for forms. A **controlled component** is an input element (like `<input>`) whose value is controlled by React state.

The "control loop" works like this:

  1. The input's `value` prop is set to a variable from `useState`.
  2. The user types a character.
  3. The `onChange` event fires.
  4. The event handler function is called.
  5. Inside the handler, we use `event.target.value` to get the new text.
  6. We call the state's setter function (e.g., `setText`) with this new text.
  7. React re-renders the component.
  8. The input's `value` is now updated from the state, showing the new character.
function NameForm() {
  const [name, setName] = useState('');

  function handleChange(event) {
    // 'event.target.value' contains the current text in the input
    setName(event.target.value);
  }

  return (
    <>
      <input value={name} onChange={handleChange} />
      <p>Your name is: {name}</p>
    </>
  );
}

3. The Submit Event: `onSubmit`

To handle form submissions, you should always use the `onSubmit` event on the `<form>` tag itself, not an `onClick` on the submit button. This ensures the form submits even if the user hits "Enter" on their keyboard.

The most important part of `onSubmit` is calling `event.preventDefault()`. By default, submitting a form causes the browser to reload the entire page. `preventDefault()` stops this behavior, allowing you to handle the submission with JavaScript and React state.

function MyForm() {
  const [name, setName] = useState('');

  function handleSubmit(event) {
    // This is ESSENTIAL!
    event.preventDefault();
    
    alert('A name was submitted: ' + name);
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Name:
        <input value={name} onChange={(e) => setName(e.target.value)} />
      </label>
      <button type="submit">Submit</button>
    </form>
  );
}

4. Passing Arguments to Handlers

What if you have a list of items and need to know *which* one was clicked? You can't just write `onClick={handleClick(item.id)}`. Instead, you use an inline arrow function:

function ItemList({ items }) {
  function handleItemClick(id) {
    console.log('Clicked item with id:', id);
  }

  return (
    <ul>
      {items.map(item => (
        <li key={item.id} onClick={() => handleItemClick(item.id)}>
          {item.name}
        </li>
      ))}
    </ul>
  );
}

Here, `onClick` is given an arrow function. That function is *not* called until the click happens. When it *is* called, it then executes your `handleItemClick` function with the correct `id`.

Key Takeaway: React's event system is all about managing state. User actions trigger events, events call handler functions, and handler functions update the state. This one-way data flow is the heart of React.

React Events Glossary

Event
An action that occurs in the browser, such as a user clicking a button, typing in a box, or submitting a form.
Event Handler
The function you provide to React to be executed when a specific event occurs. (e.g., `handleClick` in `onClick={handleClick}`).
SyntheticEvent
A cross-browser wrapper around the browser's native event object. React passes this to your event handlers, ensuring consistent behavior across all browsers (e.g., `e` in `(e) => ...`).
`onClick`
A prop for handling mouse clicks on an element.
`onChange`
A prop for handling value changes on form elements like `<input>`, `<textarea>`, and `<select>`. It fires on every keystroke.
`onSubmit`
A prop for handling the submission of a `<form>` element. It's triggered by a button with `type="submit"` or by pressing "Enter" in an input.
Controlled Component
A form input element whose value is controlled by React state. The input's `value` prop is set from state, and its `onChange` handler updates that state.
Uncontrolled Component
A form input element that stores its own value in the DOM. You typically use a `ref` to get its value when needed, rather than handling every change.
`e.preventDefault()`
A method on the event object (`e`) that stops the browser's default behavior. For `onSubmit`, it prevents a page reload. For a link, it prevents navigation.
`e.stopPropagation()`
A method on the event object that stops the event from "bubbling" up to parent elements.
`e.target`
A property on the event object that refers to the DOM element that triggered the event (e.g., the `<input>` element itself).
`e.target.value`
The current value of the element that triggered the event. This is most commonly used with `onChange` to get the text from an `<input>`.
Event Bubbling
The default behavior of events where an event triggered on a child element will also trigger on all of its parent elements, "bubbling" up the DOM tree.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching React and building robust, interactive web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest React documentation and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!