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.

🛒
UrbanCart Example
Add to Cart with onClick

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.

AddToCartButton.jsx Pass reference, not call
No parentheses! Writing 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.

SearchBar.jsx Controlled input with onChange
e.target.value: The event object 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.

CheckoutForm.jsx Always e.preventDefault()
Always preventDefault(): Without it, the browser sends a GET/POST request and reloads the page. Your React app loses all state. This is the #1 mistake beginners make with form handling.

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.

HandlerPatterns.jsx When to use each
Naming convention: React developers prefix handlers with 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.

ProductList.jsx Arrow function wrapper
How argument passing works
User clicks button
→
Arrow function runs
→
Calls addToCart(id)
→
State updates

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"
Cross-browser guarantee: React normalizes events so you never need browser-specific workarounds. Whether your UrbanCart customer uses Chrome in Nairobi or Firefox in Hyderabad, e.target.value works the same way.
SyntheticEvent.jsx Same API as native events

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.

⚛️ Try It Yourself — Cart Counter + Product Search
Output
Experiment: Try changing 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.

⚛️ Try It Yourself — Contact Form with Validation
Output
Try it: Submit the form empty to see validation errors. Then fill in a name but leave email blank. Finally, enter a valid name and email to see the success message. This is the pattern every UrbanCart form follows.

Test your knowledge

Question 1 of 5
What happens if you write onClick={handleClick()} with parentheses?
Question 2 of 5
How do you read what a user typed into an <input> inside an onChange handler?
Question 3 of 5
Why must you call e.preventDefault() in an onSubmit handler?
Question 4 of 5
How do you pass an argument like product.id to a click handler?
Question 5 of 5
What are React's synthetic events?
0
/5
Great job!

🏆 Challenge: UrbanCart interactive product page

Combine everything you learned into one component:

  1. Render a list of 5 products. Each has an "Add to Cart" onClick handler that passes the product ID.
  2. Show a cart count that updates on every add. Use useState with the functional updater.
  3. Add a search <input> with onChange that filters the product list in real time.
  4. Add a "Checkout" <form> with onSubmit, preventDefault, and basic validation.

Bonus: Add a "Remove from Cart" button that decrements the count (but never below 0).

Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →