🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

Synthetic Events

AI & DATA SCIENCE // synthetic-events

SyntheticEvent is React's cross-browser wrapper around the browser's native event object, providing a consistent API regardless of which browser is actually running the code.

Syntax

function handleEvent(syntheticEvent) {
  syntheticEvent.preventDefault();
}

Deep Dive Course

Every event handler in React receives a SyntheticEvent instance rather than the browser's raw native event directly, which normalizes properties and methods, like .target, .preventDefault(), and .stopPropagation(), to behave identically across different browsers, insulating your code from the various cross-browser inconsistencies that existed in native event handling historically. If genuinely necessary, the original native browser event is still accessible via the SyntheticEvent's .nativeEvent property, though this is rarely needed in typical application code.

1Understanding Synthetic Events

Every event handler in React receives a SyntheticEvent instance rather than the browser's raw native event directly, which normalizes properties and methods, like .target, .preventDefault(), and .stopPropagation(), to behave identically across different browsers, insulating your code from the various cross-browser inconsistencies that existed in native event handling historically. If genuinely necessary, the original native browser event is still accessible via the SyntheticEvent's .nativeEvent property, though this is rarely needed in typical application code.

💡

You almost never need to reach for event.nativeEvent — the SyntheticEvent object already exposes the same standard properties and methods you'd expect, normalized consistently across browsers, for the vast majority of use cases.

editor.html
function Input() {
  const handleChange = (event) => {
    console.log(event.constructor.name);
    console.log('Value:', event.target.value);
  };
  return <input onChange={handleChange} />;
}
localhost:3000

2Practical Example

Here is a real-world application of Synthetic Events showing how it is used in production React code.

editor.html
function Link() {
  const handleClick = (event) => {
    console.log('Native event available:', event.nativeEvent instanceof MouseEvent);
  };
  return <a href="#" onClick={handleClick}>Click</a>;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Synthetic Events:

1. Use the SyntheticEvent's normalized properties and methods, like .target and .preventDefault(), for everyday event handling rather than reaching for the native event directly

2. Access event.nativeEvent only for the rare cases needing a browser-specific native event property that SyntheticEvent doesn't expose

3. Read any needed event properties synchronously inside the handler itself, rather than storing the whole event object to inspect asynchronously later, since older React versions pooled and reused event objects

⚠️

Tip: You almost never need to reach for event.nativeEvent — the SyntheticEvent object already exposes the same standard properties and methods you'd expect, normalized consistently across browsers, for the vast majority of use cases.

editor.html
function Input() {
  const handleChange = (event) => {
    console.log(event.constructor.name);
    console.log('Value:', event.target.value);
  };
  return <input onChange={handleChange} />;
}
localhost:3000

Examples

Example 01Basic Usage
function Input() {
  const handleChange = (event) => {
    console.log(event.constructor.name);
    console.log('Value:', event.target.value);
  };
  return <input onChange={handleChange} />;
}
Example 02Advanced Example
function Link() {
  const handleClick = (event) => {
    console.log('Native event available:', event.nativeEvent instanceof MouseEvent);
  };
  return <a href="#" onClick={handleClick}>Click</a>;
}

Best Practices

  • Use the SyntheticEvent's normalized properties and methods, like .target and .preventDefault(), for everyday event handling rather than reaching for the native event directly
  • Access event.nativeEvent only for the rare cases needing a browser-specific native event property that SyntheticEvent doesn't expose
  • Read any needed event properties synchronously inside the handler itself, rather than storing the whole event object to inspect asynchronously later, since older React versions pooled and reused event objects

Interview Question

Why does React wrap the browser's native event object in a SyntheticEvent rather than just passing the native event directly to handlers?

Hint: Think about how different browsers historically implemented the same events with subtly different property names, method availability, or timing quirks.

Historically, different browsers implemented native DOM events with meaningful inconsistencies, different property names for conceptually the same piece of information, different availability of certain methods, and different timing or bubbling quirks, which meant code written directly against native browser events often needed extra conditional logic just to work reliably everywhere. SyntheticEvent normalizes all of this into one single, consistent API that behaves identically regardless of which specific browser is actually running the code, so a developer's event-handling code only ever needs to be written once, against SyntheticEvent's stable interface, rather than accounting for browser-specific differences directly. This abstraction layer is exactly what let React provide a genuinely consistent, predictable event-handling experience across browsers, insulating application code from having to know or care about the underlying native inconsistencies that SyntheticEvent quietly smooths over.

Exercises

MediumPractice using Synthetic Events in a real scenario.
View Solution
function Input() {
  const handleChange = (event) => {
    console.log(event.constructor.name);
    console.log('Value:', event.target.value);
  };
  return <input onChange={handleChange} />;
}

Frequently Asked Questions

Why does React wrap the browser's native event object in a SyntheticEvent rather than just passing the native event directly to handlers?

Historically, different browsers implemented native DOM events with meaningful inconsistencies, different property names for conceptually the same piece of information, different availability of certain methods, and different timing or bubbling quirks, which meant code written directly against native browser events often needed extra conditional logic just to work reliably everywhere. SyntheticEvent normalizes all of this into one single, consistent API that behaves identically regardless of which specific browser is actually running the code, so a developer's event-handling code only ever needs to be written once, against SyntheticEvent's stable interface, rather than accounting for browser-specific differences directly. This abstraction layer is exactly what let React provide a genuinely consistent, predictable event-handling experience across browsers, insulating application code from having to know or care about the underlying native inconsistencies that SyntheticEvent quietly smooths over.

Related Functions

events-in-reactonclickcustom-events