Event Object in Callbacks
What you'll learn: How to access the browser's event object when React calls your callback function from a child component's event handler.
The Core Idea
When you pass a callback function as a prop (like onClick or onChange), React automatically passes the synthetic event object as the first argument to your function. This event object contains useful information about what just happened—which key was pressed, what the input's new value is, the coordinates of a click, and more.
You've already seen callbacks like onClick={() => doSomething()}, but you can also receive the event:
function Parent() {
function handleClick(event) {
console.log('Button clicked at:', event.clientX, event.clientY);
}
return <button onClick={handleClick}>Click me</button>;
}
Notice we didn't use an arrow function wrapper here—we passed handleClick directly. React calls it with the event object automatically.
Accessing Event Properties
The event object has properties like:
event.target— the DOM element that triggered the eventevent.target.value— common for input fieldsevent.clientX,event.clientY— mouse coordinatesevent.key— which keyboard key was pressed
function handleChange(event) {
console.log('User typed:', event.target.value);
}
<input onChange={handleChange} />
When You Need Both Event and Custom Data
If you need the event and want to pass extra arguments, use an inline arrow function:
<button onClick={(event) => handleClick(event, customId)}>
Delete
</button>
Key Takeaway: React automatically passes the synthetic event object as the first argument to callback functions, giving you access to details about the user interaction—no special syntax needed unless you're also passing custom arguments.