React & Modern Frontend (445 Qs)•Part 1: Core React, JSX & Component Architecture (1 - 77)
How events are different in React?
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>;
}