This reference guide documents the SyntheticEvent
wrapper that forms part of React’s Event System. See the Handling Events guide to learn more.
Your event handlers will be passed instances of SyntheticEvent
, a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event, including stopPropagation()
and preventDefault()
, except the events work identically across all browsers.
If you find that you need the underlying browser event for some reason, simply use the nativeEvent
attribute to get it. Every SyntheticEvent
object has the following attributes:
boolean bubblesboolean cancelableDOMEventTarget currentTargetboolean defaultPreventednumber eventPhaseboolean isTrustedDOMEvent nativeEventvoid preventDefault()boolean isDefaultPrevented()void stopPropagation()boolean isPropagationStopped()void persist()DOMEventTarget targetnumber timeStampstring type
Note:
As of v0.14, returning
false
from an event handler will no longer stop event propagation. Instead,e.stopPropagation()
ore.preventDefault()
should be triggered manually, as appropriate.
The SyntheticEvent
is pooled. This means that the SyntheticEvent
object 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.
function onClick(event) {console.log(event); // => nullified object.console.log(event.type); // => "click"const eventType = event.type; // => "click"setTimeout(function () {console.log(event.type); // => nullconsole.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 call
event.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.
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, append Capture
to the event name; for example, instead of using onClick
, you would use onClickCapture
to handle the click event in the capture phase.
Event names:
onCopy onCut onPaste
Properties:
DOMDataTransfer clipboardData
Event names:
onCompositionEnd onCompositionStart onCompositionUpdate
Properties:
string data
Event names:
onKeyDown onKeyPress onKeyUp
Properties:
boolean altKeynumber charCodeboolean ctrlKeyboolean getModifierState(key)string keynumber keyCodestring localenumber locationboolean metaKeyboolean repeatboolean shiftKeynumber which
The key
property can take any of the values documented in the DOM Level 3 Events spec.
Event names:
onFocus onBlur
These focus events work on all elements in the React DOM, not just form elements.
Properties:
DOMEventTarget relatedTarget
Event names:
onChange onInput onInvalid onReset onSubmit
For more information about the onChange event, see Forms.
Event names:
onError onLoad
Event names:
onClick onContextMenu onDoubleClick onDrag onDragEnd onDragEnter onDragExitonDragLeave onDragOver onDragStart onDrop onMouseDown onMouseEnter onMouseLeaveonMouseMove onMouseOut onMouseOver onMouseUp
The onMouseEnter
and onMouseLeave
events propagate from the element being left to the one being entered instead of ordinary bubbling and do not have a capture phase.
Properties:
boolean altKeynumber buttonnumber buttonsnumber clientXnumber clientYboolean ctrlKeyboolean getModifierState(key)boolean metaKeynumber pageXnumber pageYDOMEventTarget relatedTargetnumber screenXnumber screenYboolean shiftKey
Event names:
onPointerDown onPointerMove onPointerUp onPointerCancel onGotPointerCaptureonLostPointerCapture onPointerEnter onPointerLeave onPointerOver onPointerOut
The onPointerEnter
and onPointerLeave
events 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 the W3 spec, pointer events extend Mouse Events with the following properties:
number pointerIdnumber widthnumber heightnumber pressurenumber tangentialPressurenumber tiltXnumber tiltYnumber twiststring pointerTypeboolean 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 of react-dom
.
If your application requires pointer events, we recommend adding a third party pointer event polyfill.
Event names:
onSelect
Event names:
onTouchCancel onTouchEnd onTouchMove onTouchStart
Properties:
boolean altKeyDOMTouchList changedTouchesboolean ctrlKeyboolean getModifierState(key)boolean metaKeyboolean shiftKeyDOMTouchList targetTouchesDOMTouchList touches
Event names:
onScroll
Properties:
number detailDOMAbstractView view
Event names:
onWheel
Properties:
number deltaModenumber deltaXnumber deltaYnumber deltaZ
Event names:
onAbort onCanPlay onCanPlayThrough onDurationChange onEmptied onEncryptedonEnded onError onLoadedData onLoadedMetadata onLoadStart onPause onPlayonPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspendonTimeUpdate onVolumeChange onWaiting
Event names:
onLoad onError
Event names:
onAnimationStart onAnimationEnd onAnimationIteration
Properties:
string animationNamestring pseudoElementfloat elapsedTime
Event names:
onTransitionEnd
Properties:
string propertyNamestring pseudoElementfloat elapsedTime
Event names:
onToggle