Introduction
Synthetic events let React handle events in a simple and consistent way across all browsers.
Synthetic events let React handle events in a simple and consistent way across all browsers.
function handleClick(event) { // event is a SyntheticEvent console.log(event.type); } <button onClick={handleClick}>Click me</button>
function handleClick(event) { alert('Button clicked!'); } <button onClick={handleClick}>Click me</button>
function handleChange(event) { console.log('Input value:', event.target.value); } <input type="text" onChange={handleChange} />
function handleKeyDown(event) { if (event.key === 'Enter') { alert('Enter key pressed'); } } <input onKeyDown={handleKeyDown} />
This React component shows how synthetic events work for a button click and input change. It updates a message below based on the event.
import React, { useState } from 'react'; export default function SyntheticEventExample() { const [message, setMessage] = useState(''); function handleClick(event) { setMessage(`Clicked button with event type: ${event.type}`); } function handleInputChange(event) { setMessage(`Input changed to: ${event.target.value}`); } return ( <main> <button onClick={handleClick} aria-label="Click me button">Click me</button> <input type="text" onChange={handleInputChange} aria-label="Type something" /> <p>{message}</p> </main> ); }
Synthetic events are pooled for performance, so you cannot use them asynchronously unless you call event.persist().
Always use event.target to get the element that triggered the event.
Synthetic events make event handling consistent across browsers.
They wrap native events and provide the same interface.
Use them by attaching handlers like onClick, onChange, onKeyDown in React components.
SyntheticEvent?onClick.onClick correctly. <button onclick={handleClick}>Click me</button> uses lowercase onclick which is invalid in JSX. <button OnClick={handleClick}>Click me</button> has incorrect capitalization. <button on-click={handleClick}>Click me</button> uses hyphen which is invalid.function MyButton() {
function handleClick(event) {
alert(event.type);
}
return <button onClick={handleClick}>Click me</button>;
}event.type property contains the type of the event, which for a click event is the string "click" in lowercase.handleClick is called and alerts event.type, so it shows "click".function Form() {
function handleSubmit(event) {
event.preventdefault();
console.log('Submitted');
}
return <form onSubmit={handleSubmit}><button type="submit">Send</button></form>;
}event.preventDefault() with a capital D. The code uses lowercase 'd', which causes an error.onSubmit. The button type should be button, not submit is incorrect because submit button type is correct. Missing return statement in handleSubmit is irrelevant since no return needed in handler.function Input() {
function handleKeyDown(event) {
// What condition should go here?
}
return <input onKeyDown={handleKeyDown} />;
}event.key which returns the key name as a string, e.g., 'Enter'. This is preferred over deprecated keyCode.event.key === 'Enter' which is modern and recommended. if (event.keyCode === 13) { /* handle Enter */ } uses keyCode which is deprecated. Options A, B, and D use deprecated or invalid properties.Pass the test and this pattern moves unknown