1. onClick — responding to clicks
In React, you attach event handlers directly in JSX using camelCase props like onClick. You pass a function reference, not a function call. This is the most important rule to remember.
Every "Add to Cart" button on UrbanCart needs an onClick handler. When the user clicks, the handler updates the cart count. No addEventListener, no querySelector — just a prop on the JSX element.
onClick={handleClick()} calls the function immediately on every render. You want onClick={handleClick} — pass the reference so React calls it only when the user actually clicks.
React way
<button onClick={handleClick}>
Declarative. Handler lives in the component. No manual cleanup.
Vanilla JS way
btn.addEventListener('click', fn)
Imperative. Requires querySelector. Must removeEventListener to avoid leaks.
2. onChange — controlled inputs
The onChange handler fires every time the user types in an input, selects an option, or toggles a checkbox. Combined with useState, it creates controlled components — inputs whose value is always driven by React state.
e gives you the input's current value through e.target.value. This is the same API as vanilla JS — React's synthetic events mirror native events exactly.
In UrbanCart, the search bar uses onChange to filter products in real time. Every keystroke updates state, which re-renders the filtered list. No manual DOM manipulation needed.
3. onSubmit — handling forms
For forms, attach onSubmit to the <form> element. The first thing your handler must do is call e.preventDefault() to stop the browser from reloading the page.
4. Handler patterns — named vs inline
You have two ways to define event handlers: as a named function defined above the return, or as an inline arrow function right in the JSX. Both work. Choose based on complexity.
Named handler
Best for multi-line logic, reusable handlers, or when you need to test the function separately. Keeps JSX clean and readable.
Inline arrow
Best for one-liners like onChange={(e) => setName(e.target.value)}. Convenient but gets messy if the logic grows beyond one expression.
handle — handleClick, handleSubmit, handleChange. Props that accept handlers use on — onClick, onSubmit. This keeps your code instantly readable.
5. Passing arguments to handlers
What if your handler needs data — like which product was clicked? You cannot write onClick={addToCart(product.id)} because that calls the function immediately. Instead, wrap it in an arrow function.
The arrow function () => addToCart(product.id) creates a new function that waits to be called. React calls it when the user clicks, and at that point it invokes addToCart with the correct product ID.
6. Synthetic events
React does not give you raw browser events. Instead, it wraps them in SyntheticEvent objects that have the same API — target, preventDefault(), stopPropagation() — but work identically across all browsers.
| Property / Method | What it does | UrbanCart example |
|---|---|---|
e.target |
The DOM element that triggered the event | Get the clicked button or input value |
e.target.value |
Current value of an input/select | Read what user typed in search bar |
e.preventDefault() |
Stop default browser action | Prevent form from reloading page |
e.stopPropagation() |
Stop event from bubbling to parent | Click inside modal without closing it |
e.type |
Event type as a string | "click", "change", "submit" |
e.target.value works the same way.
Try It Yourself #1
Build an UrbanCart feature: an "Add to Cart" button with a live counter, plus a search input that filters a product list in real time. Edit the code and hit Run to see changes.
setCartCount(cartCount + 1) to setCartCount(prev => prev + 1). Both work here, but the functional form is safer when multiple updates happen in rapid succession.
Try It Yourself #2
Build a simple UrbanCart contact form with onSubmit, field validation, and a success message. The form prevents page reload, checks that fields are filled, and shows a confirmation.
Test your knowledge
onClick={handleClick()} with parentheses?<input> inside an onChange handler?e.preventDefault() in an onSubmit handler?product.id to a click handler?🏆 Challenge: UrbanCart interactive product page
Combine everything you learned into one component:
- Render a list of 5 products. Each has an "Add to Cart"
onClickhandler that passes the product ID. - Show a cart count that updates on every add. Use
useStatewith the functional updater. - Add a search
<input>withonChangethat filters the product list in real time. - Add a "Checkout"
<form>withonSubmit,preventDefault, and basic validation.
Bonus: Add a "Remove from Cart" button that decrements the count (but never below 0).
You just wired up React events.
Now build production-grade interfaces.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →