Hello! Welcome back to our module on building dynamic UIs.
In the last lesson, we learned the fundamentals of making our components interactive by attaching event handlers like onClick and onChange. You saw how to pass a function to these props and how to use onChange to update a component's state based on user input. We even had a small preview of event.target.value.
Today, we're going to zoom in on that event object. It's the key that unlocks a much deeper level of control over user interactions. Our goal for this lesson is to use the event object to access event details and prevent default browser behavior. We'll explore what this object is, how to get useful information from it, and how to use its built-in methods to manage form submissions and other browser actions.
1. The React SyntheticEvent Object
In the previous lesson, you saw this function signature:
function handleChange(event) {
// ...
}
Whenever you trigger an event in a React application (like a click or a key press), your event handler function automatically receives one argument. By convention, we call it event or simply e.
This object isn't the standard event object that browsers provide. Instead, it's a React SyntheticEvent. This is a cross-browser wrapper around the native browser event. It's beneficial because it guarantees that the event object will have a consistent interface and behavior across all browsers (like Chrome, Firefox, Safari, etc.), saving you from having to write code to handle browser-specific quirks.
This synthetic event object is packed with useful information. The image below shows what it looks like when you log it to the console after a click.

As you can see, it has properties like type (which would be 'click' in this case) and, most importantly, target.
2. Accessing Event Details with event.target
The event.target property is arguably the most useful piece of information on the event object. It refers to the DOM element that initiated the event. Once you have the target, you can read any of its properties, such as its value, name, or id.
Reading Input Value
We already saw this in action with onChange in our last lesson. When you type into an <input> field, event.target is the input element itself, and event.target.value is the current text inside it.
Let's watch a short video that recaps this and introduces the event object more formally.
This Is How You Handle Events In A React App
This clip from the video 'This Is How You Handle Events In A React App' clearly demonstrates accessing event.target.value from an onChange event.
Watch from 03:11 to 04:20. The narrator explains that all event handlers receive an event object and shows how to log the event.target.value of an input field with each key press.
Handling Multiple Inputs
Imagine a form with fields for a username, email, and password. You could create three separate state variables and three separate handler functions. However, a more scalable pattern is to use a single state object and a single handler function.
We can achieve this by giving each <input> a name attribute that matches a key in our state object. Then, inside our handler, we can use event.target.name to know which input is being changed and event.target.value to get its new value.
This technique is extremely common in React. The following video explains it brilliantly.
Handling Complex Multiple Input Form States in React [2 LINES ONLY]
This video from Code Bless You demonstrates a very efficient pattern for managing forms with multiple inputs using a single state object and a single handler.
Watch from 03:18 to 05:38. The first part (up to 04:23) shows how to extract name and value from e.target. The second part shows how to use these variables with the spread syntax to dynamically update the correct property in your state object.
Here's a breakdown of the code pattern you just saw. Given a state object like this:
const [formData, setFormData] = useState({
username: '',
email: ''
});
And inputs like this:
<input name="username" value={formData.username} onChange={handleChange} />
<input name="email" value={formData.email} onChange={handleChange} />
Your single handleChange function would look like this:
function handleChange(e) {
const { name, value } = e.target; // Destructuring name and value from the target
setFormData(prevFormData => ({
...prevFormData, // Copy the old state
[name]: value // Overwrite the key matching the input's name with its new value
}));
}
The [name]: value part is a computed property name. It's a powerful JavaScript feature that lets you use a variable as a property key in an object literal.
3. Preventing Default Browser Behavior
Some browser events have a default action associated with them. The classic example is the submit event on a <form> element. By default, when you submit a form (either by clicking a <button type="submit"> or pressing Enter in an input field), the browser will attempt to send the form data to a server and will reload the page.
In a React application, this is almost never what you want. You want to handle the submission using JavaScript without a page refresh. To stop this default browser behavior, you can call the preventDefault() method on the event object.
The official React documentation has a great section that explains what preventDefault() does and how it differs from other event methods. Please read it to understand the core concept.
Read the section titled 'Preventing default behavior'. Pay close attention to the example and the clear distinction made between e.stopPropagation() and e.preventDefault().
Let's see this in action. The following two clips demonstrate event.preventDefault() in the context of a form's onSubmit handler.
This Is How You Handle Events In A React App
First, let's revisit the 'This Is How You Handle Events' video. This short clip shows exactly what happens when you don't use preventDefault() and how to fix it.
Watch from 02:28 to 02:51. Notice how the page reloads before preventDefault() is added.
Handling Complex Multiple Input Form States in React [2 LINES ONLY]
Now, let's look at the 'Handling Complex Multiple Input Form' video again. This shows the same concept within the context of the multi-input form we just built.
Watch from 06:01 to 06:35. This reinforces the concept and shows the final piece of handling a form in React.
So, a typical form submission handler will look like this:
function handleSubmit(event) {
// 1. Prevent the page from reloading
event.preventDefault();
// 2. Do something with the form data, e.g., log it or send it to an API
console.log('Form submitted with data:', formData);
}
4. Stopping Event Propagation
There's another important method on the event object: stopPropagation(). To understand it, you first need to understand "event bubbling".
When you click on a button, you're also technically clicking on its parent div, its parent's parent, and so on, all the way up to the document itself. Event bubbling is the process where the event "bubbles" up the DOM tree, triggering any event handlers on its ancestor elements along the way.
Sometimes, this is undesirable. You might want to handle an event on a child element without triggering the same event handler on its parent. To do this, you can call event.stopPropagation().
The React docs provide the best explanation for this concept, using a clear example of a toolbar with buttons.
Read the sections 'Event propagation' and 'Stopping propagation'. Click the buttons in the interactive examples to see the difference between letting an event bubble and stopping it.
As the documentation states, it's crucial not to confuse these two methods:
event.preventDefault(): Prevents the browser's default action for that specific event (e.g., a form submitting).event.stopPropagation(): Prevents the event from bubbling up to parent elements.
They are unrelated and solve different problems. You'll use preventDefault() far more often, but knowing that stopPropagation() exists is essential for handling complex UI interactions.
Conclusion
You've now mastered the event object, a cornerstone of interactivity in React. You've gone from simply attaching handlers to precisely controlling them.
Key Takeaways:
- Event handlers receive a
SyntheticEventobject, which provides a consistent, cross-browser API. event.targetis your gateway to the DOM element that triggered the event, allowing you to access properties likevalueandname.- You can manage multiple inputs with a single handler by using the
nameattribute and computed property names ([name]: value). event.preventDefault()is essential for stopping default browser actions, particularly to prevent page reloads on form submission.event.stopPropagation()can be used to prevent an event from "bubbling" up and triggering handlers on parent elements.
Next Up:
We've covered how to attach handlers and how to process the event they generate. The next logical step, which we've already started doing, is to use this information to update our component's state. In the next lesson, we will focus entirely on the different patterns for updating component state in response to user events, solidifying the connection between user actions and a dynamic UI.