You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
353 lines
6.3 KiB
353 lines
6.3 KiB
11 years ago
|
---
|
||
|
id: events
|
||
8 years ago
|
title: SyntheticEvent
|
||
9 years ago
|
permalink: docs/events.html
|
||
8 years ago
|
layout: docs
|
||
|
category: Reference
|
||
11 years ago
|
---
|
||
|
|
||
7 years ago
|
This reference guide documents the `SyntheticEvent` wrapper that forms part of React's Event System. See the [Handling Events](/docs/handling-events.html) guide to learn more.
|
||
8 years ago
|
|
||
|
## Overview
|
||
11 years ago
|
|
||
|
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:
|
||
|
|
||
|
```javascript
|
||
|
boolean bubbles
|
||
|
boolean cancelable
|
||
|
DOMEventTarget currentTarget
|
||
|
boolean defaultPrevented
|
||
10 years ago
|
number eventPhase
|
||
11 years ago
|
boolean isTrusted
|
||
|
DOMEvent nativeEvent
|
||
|
void preventDefault()
|
||
9 years ago
|
boolean isDefaultPrevented()
|
||
11 years ago
|
void stopPropagation()
|
||
9 years ago
|
boolean isPropagationStopped()
|
||
11 years ago
|
DOMEventTarget target
|
||
10 years ago
|
number timeStamp
|
||
|
string type
|
||
11 years ago
|
```
|
||
|
|
||
11 years ago
|
> Note:
|
||
|
>
|
||
9 years ago
|
> As of v0.14, returning `false` from an event handler will no longer stop event propagation. Instead, `e.stopPropagation()` or `e.preventDefault()` should be triggered manually, as appropriate.
|
||
11 years ago
|
|
||
8 years ago
|
### Event Pooling
|
||
10 years ago
|
|
||
9 years ago
|
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.
|
||
10 years ago
|
As such, you cannot access the event in an asynchronous way.
|
||
|
|
||
|
```javascript
|
||
|
function onClick(event) {
|
||
|
console.log(event); // => nullified object.
|
||
|
console.log(event.type); // => "click"
|
||
8 years ago
|
const eventType = event.type; // => "click"
|
||
10 years ago
|
|
||
|
setTimeout(function() {
|
||
|
console.log(event.type); // => null
|
||
|
console.log(eventType); // => "click"
|
||
|
}, 0);
|
||
|
|
||
8 years ago
|
// 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});
|
||
10 years ago
|
}
|
||
|
```
|
||
|
|
||
|
> Note:
|
||
|
>
|
||
10 years ago
|
> 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.
|
||
11 years ago
|
|
||
|
## Supported Events
|
||
|
|
||
9 years ago
|
React normalizes events so that they have consistent properties across different browsers.
|
||
10 years ago
|
|
||
|
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.
|
||
11 years ago
|
|
||
8 years ago
|
- [Clipboard Events](#clipboard-events)
|
||
|
- [Composition Events](#composition-events)
|
||
|
- [Keyboard Events](#keyboard-events)
|
||
|
- [Focus Events](#focus-events)
|
||
|
- [Form Events](#form-events)
|
||
|
- [Mouse Events](#mouse-events)
|
||
|
- [Selection Events](#selection-events)
|
||
|
- [Touch Events](#touch-events)
|
||
|
- [UI Events](#ui-events)
|
||
|
- [Wheel Events](#wheel-events)
|
||
|
- [Media Events](#media-events)
|
||
|
- [Image Events](#image-events)
|
||
|
- [Animation Events](#animation-events)
|
||
|
- [Transition Events](#transition-events)
|
||
8 years ago
|
- [Other Events](#other-events)
|
||
8 years ago
|
|
||
|
* * *
|
||
|
|
||
|
## Reference
|
||
11 years ago
|
|
||
|
### Clipboard Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onCopy onCut onPaste
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
DOMDataTransfer clipboardData
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
11 years ago
|
|
||
9 years ago
|
### Composition Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onCompositionEnd onCompositionStart onCompositionUpdate
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
string data
|
||
|
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
11 years ago
|
### Keyboard Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onKeyDown onKeyPress onKeyUp
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
boolean altKey
|
||
9 years ago
|
number charCode
|
||
11 years ago
|
boolean ctrlKey
|
||
9 years ago
|
boolean getModifierState(key)
|
||
|
string key
|
||
|
number keyCode
|
||
|
string locale
|
||
|
number location
|
||
11 years ago
|
boolean metaKey
|
||
|
boolean repeat
|
||
|
boolean shiftKey
|
||
9 years ago
|
number which
|
||
11 years ago
|
```
|
||
|
|
||
7 years ago
|
The `key` property can take any of the values documented in the [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#named-key-attribute-values).
|
||
|
|
||
8 years ago
|
* * *
|
||
11 years ago
|
|
||
|
### Focus Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onFocus onBlur
|
||
|
```
|
||
|
|
||
8 years ago
|
These focus events work on all elements in the React DOM, not just form elements.
|
||
|
|
||
11 years ago
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
DOMEventTarget relatedTarget
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
11 years ago
|
### Form Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
7 years ago
|
onChange onInput onInvalid onSubmit
|
||
11 years ago
|
```
|
||
|
|
||
7 years ago
|
For more information about the onChange event, see [Forms](/docs/forms.html).
|
||
11 years ago
|
|
||
8 years ago
|
* * *
|
||
11 years ago
|
|
||
|
### Mouse Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
10 years ago
|
onClick onContextMenu onDoubleClick onDrag onDragEnd onDragEnter onDragExit
|
||
|
onDragLeave onDragOver onDragStart onDrop onMouseDown onMouseEnter onMouseLeave
|
||
11 years ago
|
onMouseMove onMouseOut onMouseOver onMouseUp
|
||
11 years ago
|
```
|
||
|
|
||
9 years ago
|
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.
|
||
10 years ago
|
|
||
11 years ago
|
Properties:
|
||
11 years ago
|
|
||
|
```javascript
|
||
|
boolean altKey
|
||
9 years ago
|
number button
|
||
|
number buttons
|
||
|
number clientX
|
||
|
number clientY
|
||
11 years ago
|
boolean ctrlKey
|
||
9 years ago
|
boolean getModifierState(key)
|
||
11 years ago
|
boolean metaKey
|
||
9 years ago
|
number pageX
|
||
|
number pageY
|
||
11 years ago
|
DOMEventTarget relatedTarget
|
||
9 years ago
|
number screenX
|
||
|
number screenY
|
||
11 years ago
|
boolean shiftKey
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
11 years ago
|
|
||
9 years ago
|
### Selection Events
|
||
9 years ago
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onSelect
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
9 years ago
|
### Touch Events
|
||
11 years ago
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onTouchCancel onTouchEnd onTouchMove onTouchStart
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
boolean altKey
|
||
|
DOMTouchList changedTouches
|
||
|
boolean ctrlKey
|
||
9 years ago
|
boolean getModifierState(key)
|
||
11 years ago
|
boolean metaKey
|
||
|
boolean shiftKey
|
||
|
DOMTouchList targetTouches
|
||
|
DOMTouchList touches
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
11 years ago
|
|
||
|
### UI Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onScroll
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
9 years ago
|
number detail
|
||
11 years ago
|
DOMAbstractView view
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
11 years ago
|
|
||
|
### Wheel Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onWheel
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
9 years ago
|
number deltaMode
|
||
|
number deltaX
|
||
|
number deltaY
|
||
|
number deltaZ
|
||
11 years ago
|
```
|
||
10 years ago
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
10 years ago
|
### Media Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
8 years ago
|
onAbort onCanPlay onCanPlayThrough onDurationChange onEmptied onEncrypted
|
||
|
onEnded onError onLoadedData onLoadedMetadata onLoadStart onPause onPlay
|
||
|
onPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspend
|
||
9 years ago
|
onTimeUpdate onVolumeChange onWaiting
|
||
10 years ago
|
```
|
||
10 years ago
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
10 years ago
|
### Image Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onLoad onError
|
||
|
```
|
||
9 years ago
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
|
### Animation Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onAnimationStart onAnimationEnd onAnimationIteration
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
string animationName
|
||
|
string pseudoElement
|
||
|
float elapsedTime
|
||
|
```
|
||
|
|
||
8 years ago
|
* * *
|
||
9 years ago
|
|
||
|
### Transition Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onTransitionEnd
|
||
|
```
|
||
|
|
||
|
Properties:
|
||
|
|
||
|
```javascript
|
||
|
string propertyName
|
||
|
string pseudoElement
|
||
|
float elapsedTime
|
||
|
```
|
||
8 years ago
|
|
||
8 years ago
|
* * *
|
||
|
|
||
|
### Other Events
|
||
|
|
||
|
Event names:
|
||
|
|
||
|
```
|
||
|
onToggle
|
||
|
```
|