Common components (e.g. <div>)·续3
onAbort: AnEventhandler function. Fires when the resource has not fully loaded, but not due to an error.onAbortCapture: A version ofonAbortthat fires in the capture phase.onCanPlay: AnEventhandler function. Fires when there's enough data to start playing, but not enough to play to the end without buffering.onCanPlayCapture: A version ofonCanPlaythat fires in the capture phase.onCanPlayThrough: AnEventhandler function. Fires when there's enough data that it's likely possible to start playing without buffering until the end.onCanPlayThroughCapture: A version ofonCanPlayThroughthat fires in the capture phase.onDurationChange: AnEventhandler function. Fires when the media duration has updated.onDurationChangeCapture: A version ofonDurationChangethat fires in the capture phase.onEmptied: AnEventhandler function. Fires when the media has become empty.onEmptiedCapture: A version ofonEmptiedthat fires in the capture phase.onEncrypted: AnEventhandler function. Fires when the browser encounters encrypted media.onEncryptedCapture: A version ofonEncryptedthat fires in the capture phase.onEnded: AnEventhandler function. Fires when the playback stops because there's nothing left to play.onEndedCapture: A version ofonEndedthat fires in the capture phase.onError: AnEventhandler function. Fires when the resource could not be loaded.onErrorCapture: A version ofonErrorthat fires in the capture phase.onLoadedData: AnEventhandler function. Fires when the current playback frame has loaded.onLoadedDataCapture: A version ofonLoadedDatathat fires in the capture phase.onLoadedMetadata: AnEventhandler function. Fires when metadata has loaded.onLoadedMetadataCapture: A version ofonLoadedMetadatathat fires in the capture phase.onLoadStart: AnEventhandler function. Fires when the browser started loading the resource.onLoadStartCapture: A version ofonLoadStartthat fires in the capture phase.onPause: AnEventhandler function. Fires when the media was paused.onPauseCapture: A version ofonPausethat fires in the capture phase.onPlay: AnEventhandler function. Fires when the media is no longer paused.onPlayCapture: A version ofonPlaythat fires in the capture phase.onPlaying: AnEventhandler function. Fires when the media starts or restarts playing.onPlayingCapture: A version ofonPlayingthat fires in the capture phase.onProgress: AnEventhandler function. Fires periodically while the resource is loading.onProgressCapture: A version ofonProgressthat fires in the capture phase.onRateChange: AnEventhandler function. Fires when playback rate changes.onRateChangeCapture: A version ofonRateChangethat fires in the capture phase.onResize: AnEventhandler function. Fires when video changes size.onResizeCapture: A version ofonResizethat fires in the capture phase.onSeeked: AnEventhandler function. Fires when a seek operation completes.onSeekedCapture: A version ofonSeekedthat fires in the capture phase.onSeeking: AnEventhandler function. Fires when a seek operation starts.onSeekingCapture: A version ofonSeekingthat fires in the capture phase.onStalled: AnEventhandler function. Fires when the browser is waiting for data but it keeps not loading.onStalledCapture: A version ofonStalledthat fires in the capture phase.onSuspend: AnEventhandler function. Fires when loading the resource was suspended.onSuspendCapture: A version ofonSuspendthat fires in the capture phase.onTimeUpdate: AnEventhandler function. Fires when the current playback time updates.onTimeUpdateCapture: A version ofonTimeUpdatethat fires in the capture phase.onVolumeChange: AnEventhandler function. Fires when the volume has changed.onVolumeChangeCapture: A version ofonVolumeChangethat fires in the capture phase.onWaiting: AnEventhandler function. Fires when the playback stopped due to temporary lack of data.onWaitingCapture: A version ofonWaitingthat fires in the capture phase.
Caveats
- You cannot pass both
childrenanddangerouslySetInnerHTMLat the same time. - Some events (like
onAbortandonLoad) don't bubble in the browser, but bubble in React.
ref callback function
Instead of a ref object (like the one returned by useRef), you may pass a function to the ref attribute.
<div ref={(node) => {
console.log('Attached', node);
return () => {
console.log('Clean up', node)
}
}}>
See an example of using the ref callback.
When the <div> DOM node is added to the screen, React will call your ref callback with the DOM node as the argument. When that <div> DOM node is removed, React will call your the cleanup function returned from the callback.
React will also call your ref callback whenever you pass a different ref callback. In the above example, (node) => { ... } is a different function on every render. When your component re-renders, the previous function will be called with null as the argument, and the next function will be called with the DOM node.
Parameters
node: A DOM node. React will pass you the DOM node when the ref gets attached. Unless you pass the same function reference for therefcallback on every render, the callback will get temporarily cleanup and re-create during every re-render of the component.
[!NOTE]
React 19 added cleanup functions for
refcallbacks.To support backwards compatibility, if a cleanup function is not returned from the
refcallback,nodewill be called withnullwhen therefis detached. This behavior will be removed in a future version.
Returns
- optional
cleanup function: When therefis detached, React will call the cleanup function. If a function is not returned by therefcallback, React will call the callback again withnullas the argument when therefgets detached. This behavior will be removed in a future version.
Caveats
- When Strict Mode is on, React will run one extra development-only setup+cleanup cycle before the first real setup. This is a stress-test that ensures that your cleanup logic "mirrors" your setup logic and that it stops or undoes whatever the setup is doing. If this causes a problem, implement the cleanup function.
- When you pass a different
refcallback, React will call the previous callback's cleanup function if provided. If no cleanup function is defined, therefcallback will be called withnullas the argument. The next function will be called with the DOM node.
React event object
Your event handlers will receive a React event object. It is also sometimes known as a "synthetic event".
<button onClick={e => {
console.log(e); // React event object
}} />
It conforms to the same standard as the underlying DOM events, but fixes some browser inconsistencies.
Some React events do not map directly to the browser's native events. For example in onMouseLeave, e.nativeEvent will point to a mouseout event. The specific mapping is not part of the public API and may change in the future. If you need the underlying browser event for some reason, read it from e.nativeEvent.
Properties
React event objects implement some of the standard Event properties:
bubbles: A boolean. Returns whether the event bubbles through the DOM.cancelable: A boolean. Returns whether the event can be canceled.currentTarget: A DOM node. Returns the node to which the current handler is attached in the React tree.defaultPrevented: A boolean. Returns whetherpreventDefaultwas called.eventPhase: A number. Returns which phase the event is currently in.isTrusted: A boolean. Returns whether the event was initiated by user.target: A DOM node. Returns the node on which the event has occurred (which could be a distant child).timeStamp: A number. Returns the time when the event occurred.
Additionally, React event objects provide these properties:
nativeEvent: A DOMEvent. The original browser event object.
Methods
React event objects implement some of the standard Event methods:
preventDefault(): Prevents the default browser action for the event.stopPropagation(): Stops the event propagation through the React tree.
Additionally, React event objects provide these methods:
isDefaultPrevented(): Returns a boolean value indicating whetherpreventDefaultwas called.isPropagationStopped(): Returns a boolean value indicating whetherstopPropagationwas called.persist(): Not used with React DOM. With React Native, call this to read event's properties after the event.isPersistent(): Not used with React DOM. With React Native, returns whetherpersisthas been called.
Caveats
- The values of
currentTarget,eventPhase,target, andtypereflect the values your React code expects. Under the hood, React attaches event handlers at the root, but this is not reflected in React event objects. For example,e.currentTargetmay not be the same as the underlyinge.nativeEvent.currentTarget. For polyfilled events,e.type(React event type) may differ frome.nativeEvent.type(underlying type).
AnimationEvent handler function
An event handler type for the CSS animation events.
<div
onAnimationStart={e => console.log('onAnimationStart')}
onAnimationIteration={e => console.log('onAnimationIteration')}
onAnimationEnd={e => console.log('onAnimationEnd')}
/>
Parameters
e: A React event object with these extraAnimationEventproperties:animationNameelapsedTimepseudoElement
ClipboardEvent handler function
An event handler type for the Clipboard API events.
