Defining React Node Events
Events can be added to React nodes much like events can be added to DOM nodes. In the code example below I am adding a very simple click
and mouseover
event to a React <div>
node.
Note how the property name for an event in React starts with ‘on’ and is passed in the props
argument object to the ReactElement()
function.
React creates what it calls a SyntheticEvent
for each event, which contains the details for the event. Similar to the details that are defined for DOM events. The SyntheticEvent
instance, for an event, is passed into the events handlers/callback functions. In the code below I am logging the details of a SyntheticEvent instance.
Every SyntheticEvent object instance has the following properties.
boolean bubbles
boolean cancelable
DOMEventTarget currentTarget
boolean defaultPrevented
number eventPhase
boolean isTrusted
DOMEvent nativeEvent
void preventDefault()
boolean isDefaultPrevented()
void stopPropagation()
boolean isPropagationStopped()
DOMEventTarget target
number timeStamp
string type
Additional properties are available depending upon the type/category of event that is used. For example the onClick
syntheticEvent event also contains the following 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
The table below outlines the unique SyntheticEvent properties for each type/category of events.
Event Type/Category: | Events: | Event Specific Properties: |
---|---|---|
Clipboard | onCopy, onCut, onPaste | DOMDataTransfer, clipboardData |
Composition | onCompositionEnd, onCompositionStart, onCompositionUpdate | data |
Keyboard | onKeyDown, onKeyPress, onKeyUp | altKey, charCode, ctrlKey, getModifierState(key), key, keyCode, locale, location, metaKey, repeat, shiftKey, which |
Focus | onChange, onInput, onSubmit | DOMEventTarget, relatedTarget |
Form | onFocus, onBlur | |
Mouse | onClick, onContextMenu, onDoubleClick, onDrag, onDragEnd, onDragEnter, onDragExit onDragLeave, onDragOver, onDragStart, onDrop, onMouseDown, onMouseEnter, onMouseLeave onMouseMove, onMouseOut, onMouseOver, onMouseUp | altKey, button, buttons, clientX, clientY, ctrlKey, getModifierState(key), metaKey, pageX, pageY, DOMEventTarget relatedTarget, screenX, screenY, shiftKey, |
Selection | onSelect | |
Touch | onTouchCancel, onTouchEnd, onTouchMove, onTouchStart | altKey DOMTouchList changedTouches, ctrlKey, getModifierState(key), metaKey, shiftKey, DOMTouchList targetTouches, DOMTouchList touches, |
UI | onScroll | detail, DOMAbstractView view |
Wheel | onWheel | deltaMode, deltaX, deltaY, deltaZ, |
Media | onAbort, onCanPlay, onCanPlayThrough, onDurationChange, onEmptied, onEncrypted, onEnded, onError, onLoadedData, onLoadedMetadata, onLoadStart, onPause, onPlay, onPlaying, onProgress, onRateChange, onSeeked, onSeeking, onStalled, onSuspend, onTimeUpdate, onVolumeChange, onWaiting | |
Image | onLoad, onError | |
Animation | onAnimationStart, onAnimationEnd, onAnimationIteration | animationName, pseudoElement, elapsedTime |
Transition | onTransitionEnd | propertyName, pseudoElement, elapsedTime |
Notes
- React normalizes events so that they behave consistently across different browsers.
- Events in React are triggered on the bubbling phase. To trigger an event on the capturing phase add the word “Capture” to the event name (e.g.,
onClick
would becomeonClickCapture
). - If you need the browser event details you can access this by using the
nativeEvent
property found in the SyntheticEvent object passed into React event hander/callback. - React doesn’t actually attach events to the nodes themselves, it uses event delegation.
e.stopPropagation()
ore.preventDefault()
should be triggered manually to stop event propagation instead ofreturn false;
.- Not all DOM events are provided by React. But you can still make use of them using React lifecycle methods.