This reference guide documents theSyntheticEventwrapper that forms part of React’s Event System. See theHandling Eventsguide to learn more.
Overview
Your event handlers will be passed instances ofSyntheticEvent, a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, includingstopPropagation()andpreventDefault(), except the events work identically across all browsers.
If you find that you need the underlying browser event for some reason, simply use thenativeEventattribute to get it. EverySyntheticEventobject has the following attributes:
boolean bubbles boolean cancelable DOMEventTarget currentTarget boolean defaultPrevented number eventPhase boolean isTrusted DOMEvent nativeEvent voidpreventDefault() boolean isDefaultPrevented()voidstopPropagation() boolean isPropagationStopped() DOMEventTarget target number timeStamp string type
Note:
As of v0.14, returningfalsefrom an event handler will no longer stop event propagation. Instead,e.stopPropagation()ore.preventDefault()should be triggered manually, as appropriate.
Event Pooling
TheSyntheticEventis pooled. This means that theSyntheticEventobject will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
functiononClick(event){ console.log(event);// => nullified object. console.log(event.type);// => "click"const eventType = event.type;// => "click"setTimeout(function(){ console.log(event.type);// => null console.log(eventType);// => "click"},0);// Won't work. this.state.clickEvent will only contain null values.this.setState({clickEvent: event});// You can still export event properties.this.setState({eventType: event.type});}
Note:
If you want to access the event properties in an asynchronous way, you should callevent.persist()on the event, which will remove the synthetic event from the pool and allow references to the event to be retained by user code.
Supported Events
React normalizes events so that they have consistent properties across different browsers.
The event handlers below are triggered by an event in the bubbling phase. To register an event handler for the capture phase, appendCaptureto the event name; for example, instead of usingonClick, you would useonClickCaptureto handle the click event in the capture phase.
boolean altKey number charCode boolean ctrlKey boolean getModifierState(key) string key number keyCode string locale number location boolean metaKey boolean repeat boolean shiftKey number which
TheonMouseEnterandonMouseLeaveevents propagate from the element being left to the one being entered instead of ordinary bubbling and do not have a capture phase.
Properties:
boolean altKey number button number buttons number clientX number clientY boolean ctrlKey boolean getModifierState(key) boolean metaKey number pageX number pageY DOMEventTarget relatedTarget number screenX number screenY boolean shiftKey
TheonPointerEnterandonPointerLeaveevents propagate from the element being left to the one being entered instead of ordinary bubbling and do not have a capture phase.
Properties:
As defined in theW3 spec, pointer events extendMouse Eventswith the following properties:
number pointerId number width number height number pressure number tangentialPressure number tiltX number tiltY number twist string pointerType boolean isPrimary
A note on cross-browser support:
Pointer events are not yet supported in every browser (at the time of writing this article, supported browsers include: Chrome, Firefox, Edge, and Internet Explorer). React deliberately does not polyfill support for other browsers because a standard-conform polyfill would significantly increase the bundle size ofreact-dom.
If your application requires pointer events, we recommend adding a third party pointer event polyfill.
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders({
importLoaders: 2,
sourceMap: isEnvProduction && shouldUseSourceMap
}).concat({
loader: require.resolve('sass-loader'),
options: {
includePaths: [paths.appSrc + '/styles'],
sourceMap: isEnvProduction && shouldUseSourceMap
}
}),
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true
},