React Core & Hooks (407 Qs)•General
How events are different in React?
mid LevelCode ExamplereactGeneral
Handling events in React elements has some syntactic differences:
- React event handlers are named using camelCase, rather than lowercase.
- With JSX you pass a function as the event handler, rather than a string.
Practical Code Example
jsx
// React events use camelCase and synthetic wrapper for cross-browser consistency:
function Button() {
function handleClick(e) {
e.preventDefault(); // Explicit prevention required (cannot return false)
console.log('Synthetic Event target:', e.target);
}
return <a href="#" onClick={handleClick}>Click Me</a>;
}