React JS - React Enlightenment JavaScript Syntax Extension (a.k.a, JSX) Basic React Components React Component Properties React Component State

Hire Website Designer

Learn how we work and how much your website will cost.

View category button.

Defining Events on Components

Events can be added to React nodes inside of a components render configuration option (discussed in Ch. 4 - 4.7 and Ch. 5 - 5.8).

In the code example, two React events (i.e., onClick & onMouseOver) are set on React nodes (via JSX) using what looks like component props.

Basically, React when rendering a component looks for special React prop events (e.g., onClick) and treats these props differently from other props (all react events shown in table below). Obviously the difference being, that eventing in the real DOM is being wired up behind the scenes.

Part of this wiring is auto binding the context of the handler/callback to the scope of the component instance. In the code example below the value of this inside of the handlers/callbacks will reference the component instance itself.

React supports the following events and event specific syntheticEvent properties:

Event Type/Category:Events:Event Specific Properties:

Clipboard

onCopy, onCut, onPasteDOMDataTransfer, clipboardData

Composition

onCompositionEnd, onCompositionStart, onCompositionUpdatedata

Keyboard

onKeyDown, onKeyPress, onKeyUpaltKey, charCode, ctrlKey, getModifierState(key), key, keyCode, locale, location, metaKey, repeat, shiftKey, which

Focus

onChange, onInput, onSubmitDOMEventTarget, relatedTarget

Form

onFocus, onBlur

Mouse

onClick, onContextMenu, onDoubleClick, onDrag, onDragEnd, onDragEnter, onDragExit onDragLeave, onDragOver, onDragStart, onDrop, onMouseDown, onMouseEnter, onMouseLeave onMouseMove, onMouseOut, onMouseOver, onMouseUpaltKey, button, buttons, clientX, clientY, ctrlKey, getModifierState(key), metaKey, pageX, pageY, DOMEventTarget relatedTarget, screenX, screenY, shiftKey,

Selection

onSelect

Touch

onTouchCancel, onTouchEnd, onTouchMove, onTouchStartaltKey DOMTouchList changedTouches, ctrlKey, getModifierState(key), metaKey, shiftKey, DOMTouchList targetTouches, DOMTouchList touches,

UI

onScrolldetail, DOMAbstractView view

Wheel

onWheeldeltaMode, 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, onAnimationIterationanimationName, pseudoElement, elapsedTime

Transition

onTransitionEndpropertyName, 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 become onClickCapture).
  • If you need the browser event details for a given event you can access this by using the nativeEvent property found in the SyntheticEvent object passed into React event hander/callbacks.
  • React does not actually attach events to the nodes themselves, it uses event delegation.
  • e.stopPropagation() or e.preventDefault() should be triggered manually to stop event propagation instead of returning false;.
  • Not all DOM events are provided by React. But you can still make use of them using React lifecycle methods.