You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<Canary> Fragments can also accept refs, which enable interacting with underlying DOM nodes without adding wrapper elements. See reference and usage below.</Canary>
9
+
<Canary>Fragment 也可以接收 ref,使你能够与底层 DOM 节点交互,而无需添加额外的包装元素。参见下方的参考和用法。</Canary>
- <CanaryBadge /> **optional**`ref`: A ref object (e.g. from [`useRef`](/reference/react/useRef)) or [callback function](/reference/react-dom/components/common#ref-callback). React provides a `FragmentInstance`as the ref value that implements methods for interacting with the DOM nodes wrapped by the Fragment.
33
+
- <CanaryBadge /> **可选**`ref`:一个 ref 对象(例如来自 [`useRef`](/reference/react/useRef))或[回调函数](/reference/react-dom/components/common#ref-callback)。React 提供一个 `FragmentInstance`作为 ref 值,该对象实现了用于与 Fragment 包裹的 DOM 节点交互的方法。
When you pass a ref to a fragment, React provides a `FragmentInstance`object with methods for interacting with the DOM nodes wrapped by the fragment:
37
+
当你将 ref 传递给 Fragment 时,React 会提供一个 `FragmentInstance`对象,其中包含用于与 Fragment 包裹的 DOM 节点进行交互的方法:
38
38
39
-
**Event handling methods:**
40
-
-`addEventListener(type, listener, options?)`: Adds an event listener to all first-level DOM children of the Fragment.
41
-
-`removeEventListener(type, listener, options?)`: Removes an event listener from all first-level DOM children of the Fragment.
42
-
-`dispatchEvent(event)`: Dispatches an event to a virtual child of the Fragment to call any added listeners and can bubble to the DOM parent.
39
+
**事件处理方法:**
40
+
-`addEventListener(type, listener, options?)`:向 Fragment 的所有第一级 DOM 子元素添加事件监听器。
41
+
-`removeEventListener(type, listener, options?)`:从 Fragment 的所有第一级 DOM 子元素中移除事件监听器。
42
+
-`dispatchEvent(event)`:向 Fragment 的虚拟子元素分发事件以调用任何已添加的监听器,并且可以冒泡到 DOM 父元素。
43
43
44
-
**Layout methods:**
45
-
-`compareDocumentPosition(otherNode)`: Compares the document position of the Fragment with another node.
46
-
-If the Fragment has children, the native `compareDocumentPosition`value is returned.
47
-
-Empty Fragments will attempt to compare positioning within the React tree and include `Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`.
48
-
-Elements that have a different relationship in the React tree and DOM tree due to portaling or other insertions are `Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`.
49
-
-`getClientRects()`: Returns a flat array of `DOMRect`objects representing the bounding rectangles of all children.
50
-
-`getRootNode()`: Returns the root node containing the Fragment's parent DOM node.
44
+
**布局方法:**
45
+
-`compareDocumentPosition(otherNode)`:比较 Fragment 与另一个节点的文档位置。
46
+
-如果 Fragment 有子元素,则返回原生的 `compareDocumentPosition`值。
47
+
-空的 Fragment 会尝试在 React 树中比较位置,并包含 `Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`。
48
+
-由于 portal 或其他插入方式导致在 React 树和 DOM 树中关系不同的元素,将返回 `Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`。
- <CanaryBadge /> If you want to pass `ref` to a Fragment, you can't use the `<>...</>`syntax. You have to explicitly import `Fragment` from `'react'`and render `<Fragment ref={yourRef}>...</Fragment>`.
### <CanaryBadge /> Using Fragment refs for DOM interaction {/*using-fragment-refs-for-dom-interaction*/}
245
+
### <CanaryBadge /> 使用 Fragment ref 进行 DOM 交互 {/*using-fragment-refs-for-dom-interaction*/}
246
246
247
-
Fragment refs allow you to interact with the DOM nodes wrapped by a Fragment without adding extra wrapper elements. This is useful for event handling, visibility tracking, focus management, and replacing deprecated patterns like `ReactDOM.findDOMNode()`.
247
+
Fragment ref 允许你与 Fragment 包裹的 DOM 节点进行交互,而无需添加额外的包装元素。这对于事件处理、可见性跟踪、焦点管理以及替代已弃用的模式(如 `ReactDOM.findDOMNode()`)非常有用。
248
248
249
249
```js
250
250
import { Fragment } from'react';
@@ -262,9 +262,9 @@ function ClickableFragment({ children, onClick }) {
262
262
```
263
263
---
264
264
265
-
### <CanaryBadge /> Tracking visibility with Fragment refs {/*tracking-visibility-with-fragment-refs*/}
265
+
### <CanaryBadge /> 使用 Fragment ref 跟踪可见性 {/*tracking-visibility-with-fragment-refs*/}
266
266
267
-
Fragment refs are useful for visibility tracking and intersection observation. This enables you to monitor when content becomes visible without requiring the child Components to expose refs:
267
+
Fragment ref 对于可见性跟踪和交叉观察非常有用。这使你能够监控内容何时变为可见,而无需子组件暴露 ref:
This pattern is an alternative to Effect-based visibility logging, which is an anti-pattern in most cases. Relying on Effects alone does not guarantee that the rendered Component is observable by the user.
0 commit comments