` DOM node is added to the screen, React will call your `ref` callback with the DOM `node` as the argument. When that `
` DOM node is removed, React will call your `ref` callback with `null`.
+当 `
` DOM 节点被添加到屏幕上时,React 将使用该节点作为参数调用你的 `ref` 回调函数。
-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.
+当你传递不同的` ref `回调时,React 也会调用你的 `ref` 回调。在上面的示例中,`(node) => { ... }` 在每次渲染时都是一个不同的函数。当组件重新渲染时,先前的函数将被调用并带有 `null` 参数,并且下一个函数将被调用并带有 DOM 节点。
-#### Parameters {/*ref-callback-parameters*/}
+#### 参数 {/*ref-callback-parameters*/}
-* `node`: A DOM node or `null`. React will pass you the DOM node when the ref gets attached, and `null` when the ref gets detached. Unless you pass the same function reference for the `ref` callback on every render, the callback will get temporarily detached and re-attached during every re-render of the component.
+* `node`:一个 DOM 节点或`null`。当 ref 被附加时 React 会将 DOM 节点传递给你,当引用被分离时值为 `null`。除非你在每次渲染时都传递相同的函数引用作为 `ref` 回调,否则该回调将在组件的每次重新渲染期间被暂时分离和重新连接。
-#### Returns {/*returns*/}
+#### 返回 {/*returns*/}
-Do not return anything from the `ref` callback.
+不要从 `ref` 回调函数中返回任何内容。
---
-### React event object {/*react-event-object*/}
+### React 事件对象 {/*react-event-object*/}
-Your event handlers will receive a *React event object.* It is also sometimes known as a "synthetic event".
+你的事件处理程序将接收到一个 React 事件对象。它有时也被称为“合成事件”(synthetic event)。
```js
{
- console.log(e); // React event object
+ console.log(e); // React 事件对象
}} />
```
-It conforms to the same standard as the underlying DOM events, but fixes some browser inconsistencies.
+它符合与底层 DOM 事件相同的标准,但修复了一些浏览器不一致性。
-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`.
+一些 React 事件不能直接映射到浏览器的原生事件。例如,在 `onMouseLeave` 事件中,`e.nativeEvent` 将指向 `mouseout` 事件。具体的映射关系不是公共 API 的一部分,可能会在未来发生变化。如果你需要某些原因下层浏览器事件,请从 `e.nativeEvent` 中读取它。
-#### Properties {/*react-event-object-properties*/}
+#### 属性 {/*react-event-object-properties*/}
-React event objects implement some of the standard [`Event`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event) properties:
+React 事件对象实现了一些标准的[`事件`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event)属性:
-* [`bubbles`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/bubbles): A boolean. Returns whether the event bubbles through the DOM.
-* [`cancelable`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/cancelable): A boolean. Returns whether the event can be canceled.
-* [`currentTarget`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/currentTarget): A DOM node. Returns the node to which the current handler is attached in the React tree.
-* [`defaultPrevented`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/defaultPrevented): A boolean. Returns whether `preventDefault` was called.
-* [`eventPhase`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/eventPhase): A number. Returns which phase the event is currently in.
-* [`isTrusted`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/isTrusted): A boolean. Returns whether the event was initiated by user.
-* [`target`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/target): A DOM node. Returns the node on which the event has occurred (which could be a distant child).
-* [`timeStamp`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/timeStamp): A number. Returns the time when the event occurred.
+* [`bubbles`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/bubbles):一个布尔值。返回事件是否会在 DOM 中冒泡传播。
+* [`cancelable`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/cancelable):一个布尔值。返回事件是否可以被取消。
+* [`currentTarget`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/currentTarget):一个 DOM 节点。返回当前处理程序所附加到的节点在 React 树中的位置。
+* [`defaultPrevented`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/defaultPrevented):一个布尔值。返回是否调用了 `preventDefault`。
+* [`eventPhase`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/eventPhase):一个数字。返回事件当前所处的阶段。
+* [`isTrusted`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/isTrusted):一个布尔值。返回事件是否由用户发起。
+* [`target`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/target):一个 DOM 节点。返回事件发生的节点(可能是远程子节点)。
+* [`timeStamp`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/timeStamp):一个数字。返回事件发生的时间。
-Additionally, React event objects provide these properties:
+此外,React 事件对象提供了以下属性:
-* `nativeEvent`: A DOM [`Event`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event). The original browser event object.
+* `nativeEvent`:一个 DOM[`事件`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event)。原始的浏览器事件对象。
-#### Methods {/*react-event-object-methods*/}
+#### 方法 {/*react-event-object-methods*/}
-React event objects implement some of the standard [`Event`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event) methods:
+React 事件对象实现了一些标准的 [`事件`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event) 方法:
-* [`preventDefault()`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault): Prevents the default browser action for the event.
-* [`stopPropagation()`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/stopPropagation): Stops the event propagation through the React tree.
+* [`preventDefault()`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault):阻止事件的默认浏览器行为。
+* [`stopPropagation()`](https://developer.mozilla.org/zh-CN/docs/Web/API/Event/stopPropagation):阻止事件在 React 树中的传播。
-Additionally, React event objects provide these methods:
+此外,React 事件对象提供了以下方法:
-* `isDefaultPrevented()`: Returns a boolean value indicating whether `preventDefault` was called.
-* `isPropagationStopped()`: Returns a boolean value indicating whether `stopPropagation` was 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 whether `persist` has been called.
+* `isDefaultPrevented()`:返回一个布尔值,指示是否调用了 `preventDefault` 方法。
+* `isPropagationStopped()`:返回一个布尔值,指示是否调用了 `stopPropagation` 方法。
+* `persist()`:不适用于 React DOM。在 React Native 中,调用此函数以读取事件后的属性。
+* `isPersistent()`:不适用于 React DOM。在 React Native 中,返回是否已调用 `persist`。
-#### Caveats {/*react-event-object-caveats*/}
+#### 注意事项 {/*react-event-object-caveats*/}
-* The values of `currentTarget`, `eventPhase`, `target`, and `type` reflect 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.currentTarget` may not be the same as the underlying `e.nativeEvent.currentTarget`. For polyfilled events, `e.type` (React event type) may differ from `e.nativeEvent.type` (underlying type).
+* `currentTarget`、`eventPhase`、`target` 和 `type` 的值反映了你的 React 代码所期望的值。在幕后,React 在根处附加事件处理程序,但这不会反映在 React 事件对象中。例如,e.currentTarget 可能与底层 e.nativeEvent.currentTarget 不同。对于 polyfill 的事件,e.type(React 事件类型)可能与 e.nativeEvent.type(底层类型)不同。
---
-### `AnimationEvent` handler function {/*animationevent-handler*/}
+### `AnimationsEvent` 处理函数 {/*animationsevent-handler*/}
-An event handler type for the [CSS animation](https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Animations/Using_CSS_animations) events.
+一个用于 [CSS 动画](https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Animations/Using_CSS_animations) 事件的事件处理程序类型。
```js
```
-#### Parameters {/*animationevent-handler-parameters*/}
+#### 参数 {/*animationevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`AnimationEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/AnimationEvent) properties:
- * [`animationName`](https://developer.mozilla.org/zh-CN/docs/Web/API/AnimationEvent/animationName)
- * [`elapsedTime`](https://developer.mozilla.org/zh-CN/docs/Web/API/AnimationEvent/elapsedTime)
- * [`pseudoElement`](https://developer.mozilla.org/zh-CN/docs/Web/API/AnimationEvent)
+* `e`:带有这些额外 [`animationevent`](https://developer.mozilla.org/zh-CN/docs/Web/API/animationevent) 属性的 [React事件对象](#react-event-object):
+ * [`animationName`](https://developer.mozilla.org/zh-CN/docs/Web/API/animationevent/animationName)
+ * [`elapsedTime`](https://developer.mozilla.org/zh-CN/docs/Web/API/animationevent/elapsedTime)
+ * [`pseudoElement`](https://developer.mozilla.org/zh-CN/docs/Web/API/animationevent)
---
-### `ClipboardEvent` handler function {/*clipboadevent-handler*/}
+### `ClipboardEvent` 处理函数 {/*clipboadevent-handler*/}
-An event handler type for the [Clipboard API](https://developer.mozilla.org/zh-CN/docs/Web/API/Clipboard_API) events.
+一个用于 [Clipboard_API](https://developer.mozilla.org/zh-CN/docs/Web/API/Clipboard_API) 事件的处理程序类型。
```js
```
-#### Parameters {/*clipboadevent-handler-parameters*/}
+#### 参数 {/*clipboadevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`ClipboardEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/ClipboardEvent) properties:
+* `e`:一个带有这些额外 [`ClipboardEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/ClipboardEvent) 属性的 [React事件对象](#react-event-object):
* [`clipboardData`](https://developer.mozilla.org/zh-CN/docs/Web/API/ClipboardEvent/clipboardData)
---
-### `CompositionEvent` handler function {/*compositionevent-handler*/}
+### `CompositionEvent` 处理函数 {/*compositionevent-handler*/}
-An event handler type for the [input method editor (IME)](https://developer.mozilla.org/zh-CN/docs/Glossary/Input_method_editor) events.
+一个用于 [输入法编辑器(IME)](https://developer.mozilla.org/zh-CN/docs/Glossary/Input_method_editor) 的事件处理程序类型。
```js
```
-#### Parameters {/*compositionevent-handler-parameters*/}
+#### 参数 {/*compositionevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`CompositionEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/CompositionEvent) properties:
+* `e`:一个具有这些额外[`CompositionEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/CompositionEvent) 属性的 React 事件对象
* [`data`](https://developer.mozilla.org/zh-CN/docs/Web/API/CompositionEvent/data)
---
-### `DragEvent` handler function {/*dragevent-handler*/}
+### `DragEvent` 处理 {/*dragevent-handler*/}
-An event handler type for the [HTML Drag and Drop API](https://developer.mozilla.org/zh-CN/docs/Web/API/HTML_Drag_and_Drop_API) events.
+ 一个 [HTML Drag 和 Drop API] (https://developer.mozilla.org/zh-CN/docs/Web/API/HTML_Drag_and_Drop_API) 事件的一个事件处理程序类型。
```js
<>
@@ -387,7 +387,7 @@ An event handler type for the [HTML Drag and Drop API](https://developer.mozilla
onDragStart={e => console.log('onDragStart')}
onDragEnd={e => console.log('onDragEnd')}
>
- Drag source
+ 拖拽源
{ e.preventDefault(); console.log('onDragOver'); }}
onDrop={e => console.log('onDrop')}
>
- Drop target
+ 拖拽目标
>
```
-#### Parameters {/*dragevent-handler-parameters*/}
+#### 参数 {/*dragevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`DragEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/DragEvent) properties:
+* `e`:一个带有这些额外的 [`DragEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/DragEvent) 属性的 [React事件对象](#react-event-object):
* [`dataTransfer`](https://developer.mozilla.org/zh-CN/docs/Web/API/DragEvent/dataTransfer)
- It also includes the inherited [`MouseEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) properties:
+ 它还包括继承的 [`MouseEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) 属性:
* [`altKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/altKey)
* [`button`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button)
@@ -425,17 +425,17 @@ An event handler type for the [HTML Drag and Drop API](https://developer.mozilla
* [`screenY`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/screenY)
* [`shiftKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/shiftKey)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `FocusEvent` handler function {/*focusevent-handler*/}
-
-An event handler type for the focus events.
+### `FocusEvent` 处理函数 {/*focusevent-handler*/}
+ 一个用于焦点事件的事件处理程序类型。
+
```js
console.log('onFocus')}
@@ -443,48 +443,48 @@ An event handler type for the focus events.
/>
```
-[See an example.](#handling-focus-events)
+[看一个例子](#handling-focus-events)。
-#### Parameters {/*focusevent-handler-parameters*/}
+#### 参数 {/*focusevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`FocusEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/FocusEvent) properties:
+* `e`:一个有额外 [`FocusEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/FocusEvent) 属性的 [React事件对象](#react-event-object):
* [`relatedTarget`](https://developer.mozilla.org/zh-CN/docs/Web/API/FocusEvent/relatedTarget)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `Event` handler function {/*event-handler*/}
+### `Event` 处理函数 {/*event-handler*/}
-An event handler type for generic events.
+一个通用事件的事件处理程序类型。
-#### Parameters {/*event-handler-parameters*/}
+#### 参数 {/*event-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with no additional properties.
+* `e`:一个没有额外属性的 [React 事件对象](#react-event-object)。
---
-### `InputEvent` handler function {/*inputevent-handler*/}
+### `InputEvent` 处理函数 {/*inputevent-handler*/}
-An event handler type for the `onBeforeInput` event.
+一个用于 `onBeforeInput` 事件的事件处理程序类型。
```js
console.log('onBeforeInput')} />
```
-#### Parameters {/*inputevent-handler-parameters*/}
+#### 属性 {/*inputevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`InputEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/InputEvent) properties:
+* `e`:一个带有这些额外 [`InputEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/InputEvent) 属性的 [React 事件对象](#react-event-object):
* [`data`](https://developer.mozilla.org/zh-CN/docs/Web/API/InputEvent/data)
---
-### `KeyboardEvent` handler function {/*keyboardevent-handler*/}
+### `KeyboardEvent` 处理函数 {/*keyboardevent-handler*/}
-An event handler type for keyboard events.
+一个用于键盘事件的事件处理程序类型。
```js
```
-[See an example.](#handling-keyboard-events)
+[看一个例子](#handling-keyboard-events)。
-#### Parameters {/*keyboardevent-handler-parameters*/}
+#### 参数 {/*keyboardevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`KeyboardEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent) properties:
+* `e`:一个带有这些额外的 [`KeyboardEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent) 属性的 [React event object](#react-event-object):
* [`altKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/altKey)
* [`charCode`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/charCode)
* [`code`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/code)
@@ -512,16 +512,16 @@ An event handler type for keyboard events.
* [`shiftKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/shiftKey)
* [`which`](https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/which)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `MouseEvent` handler function {/*mouseevent-handler*/}
+### `MouseEvent` 处理函数 {/*mouseevent-handler*/}
-An event handler type for mouse events.
+一个用于鼠标事件的事件处理程序类型。
```js
```
-[See an example.](#handling-mouse-events)
+[看一个例子](#handling-mouse-events)。
-#### Parameters {/*mouseevent-handler-parameters*/}
+#### 参数 {/*mouseevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`MouseEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) properties:
+* `e`:一个具有这些额外 [`鼠标事件`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) 属性的 [React event object](#react-event-object):
* [`altKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/altKey)
* [`button`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button)
* [`buttons`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/buttons)
@@ -556,16 +556,16 @@ An event handler type for mouse events.
* [`screenY`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/screenY)
* [`shiftKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/shiftKey)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `PointerEvent` handler function {/*pointerevent-handler*/}
+### `PointerEvent` 处理函数 {/*pointerevent-handler*/}
-An event handler type for [pointer events.](https://developer.mozilla.org/zh-CN/docs/Web/API/Pointer_events)
+一个 [pointer events](https://developer.mozilla.org/zh-CN/docs/Web/API/Pointer_events) 的事件处理程序类型。
```js
```
-[See an example.](#handling-pointer-events)
+[看一个例子](#handling-pointer-events)。
-#### Parameters {/*pointerevent-handler-parameters*/}
+#### 参数 {/*pointerevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`PointerEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent) properties:
+* `e`:具有这些额外 [`PointerEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent) 属性的 [React event object](#react-event-object):
* [`height`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent/height)
* [`isPrimary`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent/isPrimary)
* [`pointerId`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent/pointerId)
@@ -593,7 +593,7 @@ An event handler type for [pointer events.](https://developer.mozilla.org/zh-CN/
* [`twist`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent/twist)
* [`width`](https://developer.mozilla.org/zh-CN/docs/Web/API/PointerEvent/width)
- It also includes the inherited [`MouseEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) properties:
+ 它还包括继承的 [`MouseEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) 属性:
* [`altKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/altKey)
* [`button`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button)
@@ -612,16 +612,16 @@ An event handler type for [pointer events.](https://developer.mozilla.org/zh-CN/
* [`screenY`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/screenY)
* [`shiftKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/shiftKey)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `TouchEvent` handler function {/*touchevent-handler*/}
+### `TouchEvent` 处理函数 {/*touchevent-handler*/}
-An event handler type for [touch events.](https://developer.mozilla.org/zh-CN/docs/Web/API/Touch_events)
+ 一个用于 [touch events](https://developer.mozilla.org/zh-CN/docs/Web/API/Touch_events) 的事件处理程序类型。
```js
```
-#### Parameters {/*touchevent-handler-parameters*/}
+#### 参数 {/*touchevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`TouchEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent) properties:
+* `e`:一个带有这些额外的 [`TouchEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent) 属性的 [React event object](#react-event-object):
* [`altKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/altKey)
* [`ctrlKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/ctrlKey)
* [`changedTouches`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/changedTouches)
@@ -644,16 +644,16 @@ An event handler type for [touch events.](https://developer.mozilla.org/zh-CN/do
* [`touches`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/touches)
* [`targetTouches`](https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/targetTouches)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `TransitionEvent` handler function {/*transitionevent-handler*/}
+### `TransitionEvent` 处理函数 {/*transitionevent-handler*/}
-An event handler type for the CSS transition events.
+一个用于 CSS 过渡的事件处理程序类型。
```js
```
-#### Parameters {/*transitionevent-handler-parameters*/}
+#### 参数 {/*transitionevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`TransitionEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/TransitionEvent) properties:
+* `e`:一个带有这些额外 [`TransitionEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/TransitionEvent) 属性的 [React 事件对象](#react-event-object):
* [`elapsedTime`](https://developer.mozilla.org/zh-CN/docs/Web/API/TransitionEvent/elapsedTime)
* [`propertyName`](https://developer.mozilla.org/zh-CN/docs/Web/API/TransitionEvent/propertyName)
* [`pseudoElement`](https://developer.mozilla.org/zh-CN/docs/Web/API/TransitionEvent/pseudoElement)
---
-### `UIEvent` handler function {/*uievent-handler*/}
+### `UIEvent` 处理函数 {/*uievent-handler*/}
-An event handler type for generic UI events.
+ 一个通用 UI 事件的事件处理程序类型。
```js
```
-#### Parameters {/*uievent-handler-parameters*/}
+#### 参数 {/*uievent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+* `e`:一个带有这些额外 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性的 [React 事件对象](#react-event-object):
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
---
-### `WheelEvent` handler function {/*wheelevent-handler*/}
+### `WheelEvent` 处理函数 {/*wheelevent-handler*/}
-An event handler type for the `onWheel` event.
+一个用于 `onWheel` 事件的事件处理程序类型。
```js
```
-#### Parameters {/*wheelevent-handler-parameters*/}
+#### 参数 {/*wheelevent-handler-parameters*/}
-* `e`: A [React event object](#react-event-object) with these extra [`WheelEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent) properties:
+* `e`:带有这些额外 [`WheelEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent) 属性的 [React 事件对象](#react-event-object):
* [`deltaMode`](https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent/deltaMode)
* [`deltaX`](https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent/deltaX)
* [`deltaY`](https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent/deltaY)
* [`deltaZ`](https://developer.mozilla.org/zh-CN/docs/Web/API/WheelEvent/deltaZ)
- It also includes the inherited [`MouseEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) properties:
+ 它还包括继承的 [`鼠标事件`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent) 属性:
* [`altKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/altKey)
* [`button`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button)
@@ -726,7 +726,7 @@ An event handler type for the `onWheel` event.
* [`screenY`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/screenY)
* [`shiftKey`](https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/shiftKey)
- It also includes the inherited [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) properties:
+ 它还包括继承的 [`UIEvent`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent) 属性:
* [`detail`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/detail)
* [`view`](https://developer.mozilla.org/zh-CN/docs/Web/API/UIEvent/view)
@@ -735,15 +735,15 @@ An event handler type for the `onWheel` event.
## Usage {/*usage*/}
-### Applying CSS styles {/*applying-css-styles*/}
+### 应用 CSS 样式 {/*applying-css-styles*/}
-In React, you specify a CSS class with [`className`.](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/className) It works like the `class` attribute in HTML:
+在 React 中,你可以使用 [`className`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/className) 指定 CSS 类。它的工作方式类似于 HTML 中的 `class` 属性:
```js
```
-Then you write the CSS rules for it in a separate CSS file:
+然后你在单独的 CSS 文件中编写它的 CSS 规则:
```css
/* In your CSS */
@@ -752,9 +752,9 @@ Then you write the CSS rules for it in a separate CSS file:
}
```
-React does not prescribe how you add CSS files. In the simplest case, you'll add a [`
`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/link) tag to your HTML. If you use a build tool or a framework, consult its documentation to learn how to add a CSS file to your project.
+在最简单的情况下,你可以将 [`
`](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/link) 标签添加到 HTML 中。如果你使用构建工具或框架,请查阅其文档以了解如何将 CSS 文件添加到项目中。React 不规定如何添加 CSS 文件。
-Sometimes, the style values depend on data. Use the `style` attribute to pass some styles dynamically:
+有时,样式值取决于数据。使用 `style` 属性动态传递一些样式:
```js {3-6}
@@ -811,13 +811,13 @@ export default function Avatar({ user }) {
-#### How to apply multiple CSS classes conditionally? {/*how-to-apply-multiple-css-classes-conditionally*/}
+#### 如何有条件地应用多个 CSS 类? {/*how-to-apply-multiple-css-classes-conditionally*/}
-To apply CSS classes conditionally, you need to produce the `className` string yourself using JavaScript.
+要有条件地应用 CSS 类,你需要使用 JavaScript 自己生成 `className` 字符串。
-For example, `className={'row ' + (isSelected ? 'selected': '')}` will produce either `className="row"` or `className="row selected"` depending on whether `isSelected` is `true`.
+例如,`className={'row ' + (isSelected ? 'selected':'')}` 将会生成 `className="row"` 或者 `className="row selected"` 取决于 `isSelected` 是不是 `true`。
-To make this more readable, you can use a tiny helper library like [`classnames`:](https://github.com/JedWatson/classnames)
+为了更好的可读性,你可以使用像 [`classnames`](https://github.com/JedWatson/classnames) 这样的小助手库:
```js
import cn from 'classnames';
@@ -831,7 +831,7 @@ function Row({ isSelected }) {
}
```
-It is especially convenient if you have multiple conditional classes:
+如果你有多个条件类,则特别方便:
```js
import cn from 'classnames';
@@ -853,11 +853,11 @@ function Row({ isSelected, size }) {
---
-### Manipulating a DOM node with a ref {/*manipulating-a-dom-node-with-a-ref*/}
+### 使用 ref 操作 DOM 节点 {/*manipulating-a-dom-node-with-a-ref*/}
-Sometimes, you'll need to get the browser DOM node associated with a tag in JSX. For example, if you want to focus an ` ` when a button is clicked, you need to call [`focus()`](https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/focus) on the browser ` ` DOM node.
+有时候,你需要获取与 JSX 标签相关联的浏览器 DOM 节点。举个例子,当你希望在点击一个按钮的时候聚焦一个 ` `,你需要在浏览器的` ` DOM 节点上调用 [`focus()`](https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/focus) 方法。
-To obtain the browser DOM node for a tag, [declare a ref](/reference/react/useRef) and pass it as the `ref` attribute to that tag:
+要获取标签的浏览器 DOM 节点,请 [声明一个ref](/reference/react/useRef) 并将其作为一个 `ref` 属性传递给标签:
```js {7}
import { useRef } from 'react';
@@ -870,7 +870,7 @@ export default function Form() {
// ...
```
-React will put the DOM node into `inputRef.current` after it's been rendered to the screen.
+在渲染到屏幕后,React 会将 DOM 节点放入 `inputRef.current` 中。
@@ -897,24 +897,24 @@ export default function Form() {
-Read more about [manipulating DOM with refs](/learn/manipulating-the-dom-with-refs) and [check out more examples.](/reference/react/useRef#examples-dom)
+阅读更多关于 [使用 ref 操纵 DOM](/learn/manipulating-the-dom-with-refs) 的内容并 [查看更多示例](/reference/react/useRef#examples-dom)。
-For more advanced use cases, the `ref` attribute also accepts a [callback function.](#ref-callback)
+对于更高级的用例,`ref` 属性还可以接受 [回调函数](#ref-callback)。
---
-### Dangerously setting the inner HTML {/*dangerously-setting-the-inner-html*/}
+### 危险地设置内部 HTML {/*dangerously-setting-the-inner-html*/}
-You can pass a raw HTML string to an element like so:
+你可以像这样将原始的 HTML 字符串传递给元素:
```js
-const markup = { __html: 'some raw html
' };
+const markup = { __html:'some raw html
' };
return
;
```
-**This is dangerous. As with the underlying DOM [`innerHTML`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/innerHTML) property, you must exercise extreme caution! Unless the markup is coming from a completely trusted source, it is trivial to introduce an [XSS](https://en.wikipedia.org/wiki/Cross-site_scripting) vulnerability this way.**
+**这很危险。与底层的 DOM [`innerHTML`](https://developer.mozilla.org/zh-CN/docs/Web/API/Element/innerHTML) 属性一样,你必须极度谨慎! 除非标记语言来自完全可信的来源,否则通过这种方式引入 [XSS](https://en.wikipedia.org/wiki/Cross-site_scripting) 是容易被攻击的**。
-For example, if you use a Markdown library that converts Markdown to HTML, you trust that its parser doesn't contain bugs, and the user only sees their own input, you can display the resulting HTML like this:
+例如,如果你使用将 Markdown 转换为 HTML 的 Markdown 库,你得相信它的解析器没有漏洞,用户只能看到自己的输入,你可以像这样显示生成的 HTML:
@@ -923,11 +923,11 @@ import { useState } from 'react';
import MarkdownPreview from './MarkdownPreview.js';
export default function MarkdownEditor() {
- const [postContent, setPostContent] = useState('_Hello,_ **Markdown**!');
+ const [postContent, setPostContent] = useState('你好,**Markdown**!');
return (
<>
- Enter some markdown:
+ 输入一些 markdown:
-To see why rendering arbitrary HTML is dangerous, replace the code above with this:
+要了解为什么渲染任意 HTML 是危险的,请将上面的代码替换为此代码:
```js {1-4,7,8}
const post = {
- // Imagine this content is stored in the database.
- content: ` `
+ // 想象这个内容被存储在数据库中
+ content: ` `
};
export default function MarkdownPreview() {
- // 🔴 SECURITY HOLE: passing untrusted input to dangerouslySetInnerHTML
+ // 🔴 安全漏洞:将不受信任的输入传递给 dangerouslySetInnerHTML
const markup = { __html: post.content };
return
;
}
```
-The code embedded in the HTML will run. A hacker could use this security hole to steal user information or to perform actions on their behalf. **Only use `dangerouslySetInnerHTML` with trusted and sanitized data.**
+HTML 中嵌入的代码将会运行。黑客可以利用这个安全漏洞窃取用户信息或代表他们执行操作。 **只有在使用受信任和经过消毒的数据时才能使用 `dangerouslySetInnerHTML`**。
---
-### Handling mouse events {/*handling-mouse-events*/}
+### 处理鼠标事件 {/*handling-mouse-events*/}
-This example shows some common [mouse events](#mouseevent-handler) and when they fire.
+这个例子展示了一些常见的 [鼠标事件](#mouseevent-handler) 以及它们触发的时机。
@@ -1048,9 +1048,9 @@ input { margin-left: 10px; }
---
-### Handling pointer events {/*handling-pointer-events*/}
+### 处理指针事件 {/*handling-pointer-events*/}
-This example shows some common [pointer events](#pointerevent-handler) and when they fire.
+这个例子展示了一些常见的 [指针事件](#pointerevent-handler) 以及它们触发的时机。
@@ -1096,9 +1096,9 @@ input { margin-left: 10px; }
---
-### Handling focus events {/*handling-focus-events*/}
+### 处理焦点事件 {/*handling-focus-events*/}
-In React, [focus events](#focusevent-handler) bubble. You can use the `currentTarget` and `relatedTarget` to differentiate if the focusing or blurring events originated from outside of the parent element. The example shows how to detect focusing a child, focusing the parent element, and how to detect focus entering or leaving the whole subtree.
+在 React 中,[焦点事件](#focusevent-handler) 冒泡。你可以使用 `currentTarget` 和 `relatedTarget` 来区分焦点或模糊事件是否起源于父元素之外。该示例展示了如何检测子元素的聚焦、父级元素的聚焦,以及如何检测整个子树的聚焦进入或离开。
@@ -1114,7 +1114,7 @@ export default function FocusExample() {
console.log('focused child', e.target.name);
}
if (!e.currentTarget.contains(e.relatedTarget)) {
- // Not triggered when swapping focus between children
+ // 在子元素之间切换焦点时不会触发
console.log('focus entered parent');
}
}}
@@ -1125,7 +1125,7 @@ export default function FocusExample() {
console.log('unfocused child', e.target.name);
}
if (!e.currentTarget.contains(e.relatedTarget)) {
- // Not triggered when swapping focus between children
+ // 在子元素之间切换焦点时不会触发
console.log('focus left parent');
}
}}
@@ -1152,9 +1152,9 @@ input { margin-left: 10px; }
---
-### Handling keyboard events {/*handling-keyboard-events*/}
+### 处理键盘事件 {/*handling-keyboard-events*/}
-This example shows some common [keyboard events](#keyboardevent-handler) and when they fire.
+这个例子展示了一些常见的 [键盘事件](#keyboardevent-handler) 以及它们触发的时机。