Performance: Synthetic events
Synthetic events affect how user interactions are handled and can impact input responsiveness and event processing speed.
function Button() { return <button onClick={() => console.log('Clicked')}>Click me</button>; }
function Button() { return <button onClick={(e) => { e.persist(); console.log('Clicked'); }}>Click me</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using React synthetic events normally | No extra DOM ops | 0 | 0 | [OK] Good |
| Calling e.persist() on synthetic events | No extra DOM ops | 0 | 0 | [!] OK - higher memory use |
| Using native DOM events directly in React | No extra DOM ops | 0 | 0 | [!] OK - bypasses React pooling |
| Heavy event handler logic inside synthetic events | No extra DOM ops | 0 | 0 | [X] Bad - slows input responsiveness |
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