These docs are old and won’t be updated. Go toreact.dev for the new React docs.
These new documentation pages teach modern React and include live examples:
This reference guide documents theSyntheticEvent wrapper that forms part of React’s Event System. See theHandling Events guide to learn more.
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 thenativeEvent attribute to get it. The synthetic events are different from, and do not map directly to, the browser’s native events. For example inonMouseLeaveevent.nativeEvent will point to amouseout event. The specific mapping is not part of the public API and may change at any time. EverySyntheticEvent object has the following attributes:
boolean bubblesboolean cancelableDOMEventTarget currentTargetboolean defaultPreventednumber eventPhaseboolean isTrustedDOMEvent nativeEventvoidpreventDefault()booleanisDefaultPrevented()voidstopPropagation()booleanisPropagationStopped()voidpersist()DOMEventTarget targetnumber timeStampstring typeNote:
As of v17,
e.persist()doesn’t do anything because theSyntheticEventis no longerpooled.
Note:
As of v0.14, returning
falsefrom an event handler will no longer stop event propagation. Instead,e.stopPropagation()ore.preventDefault()should be triggered manually, as appropriate.
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, appendCapture to the event name; for example, instead of usingonClick, you would useonClickCapture to handle the click event in the capture phase.
Event names:
onCopy onCut onPasteProperties:
DOMDataTransfer clipboardDataEvent names:
onCompositionEnd onCompositionStart onCompositionUpdateProperties:
string dataEvent names:
onKeyDown onKeyPress onKeyUpProperties:
boolean altKeynumber charCodeboolean ctrlKeybooleangetModifierState(key)string keynumber keyCodestring localenumber locationboolean metaKeyboolean repeatboolean shiftKeynumber whichThekey property can take any of the values documented in theDOM Level 3 Events spec.
Event names:
onFocus onBlurThese focus events work on all elements in the React DOM, not just form elements.
Properties:
DOMEventTarget relatedTargetTheonFocus event is called when the element (or some element inside of it) receives focus. For example, it’s called when the user clicks on a text input.
functionExample(){return(<inputonFocus={(e)=>{ console.log('Focused on input');}}placeholder="onFocus is triggered when you click this input."/>)}TheonBlur event handler is called when focus has left the element (or left some element inside of it). For example, it’s called when the user clicks outside of a focused text input.
functionExample(){return(<inputonBlur={(e)=>{ console.log('Triggered because this input lost focus');}}placeholder="onBlur is triggered when you click this input and then you click outside of it."/>)}You can use thecurrentTarget andrelatedTarget to differentiate if the focusing or blurring events originated fromoutside of the parent element. Here is a demo you can copy and paste that shows how to detect focusing a child, focusing the element itself, and focus entering or leaving the whole subtree.
functionExample(){return(<divtabIndex={1}onFocus={(e)=>{if(e.currentTarget=== e.target){ console.log('focused self');}else{ console.log('focused child', e.target);}if(!e.currentTarget.contains(e.relatedTarget)){// Not triggered when swapping focus between children console.log('focus entered self');}}}onBlur={(e)=>{if(e.currentTarget=== e.target){ console.log('unfocused self');}else{ console.log('unfocused child', e.target);}if(!e.currentTarget.contains(e.relatedTarget)){// Not triggered when swapping focus between children console.log('focus left self');}}}><inputid="1"/><inputid="2"/></div>);}Event names:
onChange onInput onInvalid onReset onSubmitFor more information about the onChange event, seeForms.
Event names:
onError onLoadEvent names:
onClick onContextMenu onDoubleClick onDrag onDragEnd onDragEnter onDragExitonDragLeave onDragOver onDragStart onDrop onMouseDown onMouseEnter onMouseLeaveonMouseMove onMouseOut onMouseOver onMouseUpTheonMouseEnter andonMouseLeave 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 ctrlKeybooleangetModifierState(key)boolean metaKeynumber pageXnumber pageYDOMEventTarget relatedTargetnumber screenXnumber screenYboolean shiftKeyEvent names:
onPointerDown onPointerMove onPointerUp onPointerCancel onGotPointerCaptureonLostPointerCapture onPointerEnter onPointerLeave onPointerOver onPointerOutTheonPointerEnter andonPointerLeave 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 theW3 spec, pointer events extendMouse Events with the following properties:
number pointerIdnumber widthnumber heightnumber pressurenumber tangentialPressurenumber tiltXnumber tiltYnumber twiststring pointerTypeboolean isPrimaryA 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.
Event names:
onSelectEvent names:
onTouchCancel onTouchEnd onTouchMove onTouchStartProperties:
boolean altKeyDOMTouchList changedTouchesboolean ctrlKeybooleangetModifierState(key)boolean metaKeyboolean shiftKeyDOMTouchList targetTouchesDOMTouchList touchesEvent names:
onScrollNote
Starting with React 17, the
onScrolleventdoes not bubble in React. This matches the browser behavior and prevents the confusion when a nested scrollable element fires events on a distant parent.
Properties:
number detailDOMAbstractView viewEvent names:
onWheelProperties:
number deltaModenumber deltaXnumber deltaYnumber deltaZEvent names:
onAbort onCanPlay onCanPlayThrough onDurationChange onEmptied onEncryptedonEnded onError onLoadedData onLoadedMetadata onLoadStart onPause onPlayonPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspendonTimeUpdate onVolumeChange onWaitingEvent names:
onLoad onErrorEvent names:
onAnimationStart onAnimationEnd onAnimationIterationProperties:
string animationNamestring pseudoElementfloat elapsedTimeEvent names:
onTransitionEndProperties:
string propertyNamestring pseudoElementfloat elapsedTimeEvent names:
onToggle