Module 3.2: Event Handling & Child-Parent Communication

🎯 Learning Objectives
By the end of this lesson, you’ll master:
- Event Handling Syntax: The standard React way to capture events (
onClick,onChange,onSubmit). - Synthetic Event: Understand how React’s
eeventobjectdiffers from a native DOM Event. - Child-to-Parent Communication: The technique of passing a
functionviapropsto allow a childcomponentto send signals back up to its parentcomponent. - Basic Form Handling: How to control inputs and handle form submissions.
1. Event Handling in React
1.1. Basic Syntax
React handles events very similarly to native DOM elements, but with two key syntactical differences:
- Events use camelCase (e.g.,
onClickinstead ofonclick). - You pass a
functionreference within curly braces{}, not astring.
HTML:
<button onclick="activateLasers()">Activate Lasers</button>React:
<button onClick={activateLasers}>Activate Lasers</button>1.2. Common Pitfall: Calling the function Immediately
A common newbie mistake is adding () after the function name when assigning it to an event handler.
⛔ INCORRECT (Calls the function immediately upon render):
// React will call handleClick IMMEDIATELY when the component renders.
// Result: The function runs at the wrong time, clicks have no effect, or it causes an infinite loop.
<button onClick={handleClick()}>Click Me</button>✅ CORRECT (Passes only the function reference):
// React only calls handleClick WHEN the user clicks.
<button onClick={handleClick}>Click Me</button>✅ CORRECT (Use an Arrow Function if you need to pass parameters):
<button onClick={() => handleDelete(id)}>Delete</button>2. Event Object & Synthetic Event
When you declare an event handler, React automatically passes an object called the Event Object (often named e or event) as a parameter.

2.1. SyntheticBaseEvent
React uses a wrapper class called SyntheticEvent to encapsulate the browser’s native event. This ensures your event handling code behaves identically across all browsers (Cross-browser compatibility).
2.2. Key Properties
e.target: Theelementthat triggered the event.e.currentTarget: Theelementto which the event handler is attached.e.preventDefault(): Prevents the default browser behavior (e.g., stopping a form submission from reloading the page).e.stopPropagation(): Stops the event from bubbling up to parentcomponents.
Form Handling Example:
function LoginForm() {
const handleSubmit = (e) => {
e.preventDefault(); // 1. Prevents page reload
console.log('Form submitted!');
};
return (
<form onSubmit={handleSubmit}>
<button type="submit">Login</button>
</form>3. Child-to-Parent Communication
In React, data typically flows in one direction: from Parent down to Child via Props. So, how can a Child component report back to its Parent (e.g., if a user clicks a button in the Child, and the Parent needs to update its state)?
Solution: The Parent component passes a Function (Callback) down to the Child via Props. The Child will then call that function when needed.

3.1. The 3-Step Process
- Parent: Create a handler
function(e.g.,handleUpdate). - Parent: Pass that
functiondown to the Child viaprops(e.g.,onUpdate={handleUpdate}). - Child: Call the
functionfrompropswhen an event occurs (e.g.,onClick={props.onUpdate}).
3.2. Code Example
Parent Component:
import { useState } from 'react';
import ChildButton from './ChildButton';
export default function Dashboard() {
const [count, setCount] = useState(0);
// 1. Handler function in the Parent
const handleIncrement = () => {
setCount(count + 1);
};
return (
<div className="p-4 border">
<h1>Dashboard (Parent)</h1>
<p>Count: {count}</p>
{/* 2. Pass the function down to the Child */}
<ChildButton onIncrease={handleIncrement} />
</div>
);
}Child Component:
// Receive onIncrease prop
export default function ChildButton({ onIncrease }) {
return (
<div className="mt-4 p-2 bg-gray-100 rounded">
<h3>Control Panel (Child)</h3>
{/* 3. Call the function on click */}
<button onClick={onIncrease} className="bg-blue-500 text-white px-4 py-2 rounded">
Increase Count
</button>
</div>
);
}4. Hands-on Practice (Labs)
4.1. Lab 1: Instant Search Filter (Mirroring)
Create a search input field and a text list below it.
- When the user hasn’t typed: Display placeholder text “Start typing something…”.
- When the user types: The text below should display EXACTLY what’s being typed (Real-time Mirror), but in uppercase.

💡 Hint:
- Use a
statevariabletextto store the input value.- The
handleChangefunctionreceivese:setText(e.target.value).- For display, use
text.toUpperCase()to transform the data slightly before rendering.
4.2. Lab 2: Background Color Changer (Child -> Parent)
Build a simple background color changer application.
ColorDisplayComponent(Parent): Features a large square box displaying the current color.ColorControlsComponent(Child): Contains three buttons (🔴 Red, 🟢 Green, 🔵 Blue).- Task: Clicking a button in the Child should change the background color of the box in the Parent.

💡 Hint:
- In the Parent: Declare a
functionhandleColorChange(newColor).- Pass this
functiondown to the Child via aprop:onChangeColor={handleColorChange}.- In the Child: Call
onClick={() => onChangeColor('red')}. Don’t forget thearrow functionto passparameters!
5. Summary
- CamelCase: Always use
onClick,onChange,onSubmit, etc. FunctionReference: UseonClick={handleClick}, NOTonClick={handleClick()}.e.preventDefault(): Always use it when handling form submissions to prevent page reloads.- Callback
Props: To pass data back up from Child to Parent, pass afunctionfrom the Parent down to the Child, and have the Child call it.