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
Copy file name to clipboardExpand all lines: src/content/reference/react/Fragment.md
+30-19Lines changed: 30 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,26 +46,15 @@ Wrap elements in `<Fragment>` to group them together in situations where you nee
46
46
47
47
When you pass a `ref` to a Fragment, React provides a `FragmentInstance` object. It implements methods for interacting with the first-level DOM children wrapped by the Fragment.
48
48
49
-
```js
50
-
import { Fragment, useRef } from'react';
51
-
52
-
functionMyComponent() {
53
-
constref=useRef(null);
54
-
return (
55
-
<Fragment ref={ref}>
56
-
<div id="A"/>
57
-
<Wrapper>
58
-
<div id="B">
59
-
<div id="C"/>
60
-
</div>
61
-
</Wrapper>
62
-
<div id="D"/>
63
-
</Fragment>
64
-
);
65
-
}
66
-
```
49
+
*[`addEventListener`](#addeventlistener) and [`removeEventListener`](#removeeventlistener) manage event listeners across all first-level DOM children.
50
+
*[`dispatchEvent`](#dispatchevent) dispatches an event on the Fragment, which can bubble to the DOM parent.
51
+
*[`focus`](#focus), [`focusLast`](#focuslast), and [`blur`](#blur) manage focus across all nested children depth-first.
52
+
*[`observeUsing`](#observeusing) and [`unobserveUsing`](#unobserveusing) attach and detach `IntersectionObserver` or `ResizeObserver` instances.
53
+
*[`getClientRects`](#getclientrects) returns bounding rectangles of all first-level DOM children.
54
+
*[`getRootNode`](#getrootnode) returns the root node of the Fragment's parent.
55
+
*[`compareDocumentPosition`](#comparedocumentposition) compares the Fragment's position with another node.
67
56
68
-
In the example above, the `FragmentInstance` targets `A`, `B`, and `D`—the first-level host (DOM) children. `C` is not targeted because it is nested inside `B`. If a new host sibling is dynamically added alongside `A`, `B`, or `D`, the `FragmentInstance` automatically tracks it and applies existing event listeners and observers.
`true` if the event was not cancelled, `false` if `preventDefault()` was called.
121
114
115
+
---
116
+
122
117
#### `focus(options?)` {/*focus*/}
123
118
124
119
Focuses the first focusable DOM node in the Fragment. Unlike calling `element.focus()` on a DOM element, this method searches *all* nested children depth-first until it finds a focusable element—not just the element itself or its direct children.
@@ -135,6 +130,8 @@ fragmentRef.current.focus();
135
130
136
131
`focus` does not return anything (`undefined`).
137
132
133
+
---
134
+
138
135
#### `focusLast(options?)` {/*focuslast*/}
139
136
140
137
Focuses the last focusable DOM node in the Fragment. Searches nested children depth-first, then iterates in reverse.
`unobserveUsing` does not return anything (`undefined`).
198
201
202
+
---
203
+
199
204
#### `getClientRects()` {/*getclientrects*/}
200
205
201
206
Returns a flat array of [`DOMRect`](https://developer.mozilla.org/en-US/docs/Web/API/DOMRect) objects representing the bounding rectangles of all first-level DOM children.
An `Array<DOMRect>` containing the bounding rectangles of all children.
210
215
216
+
---
217
+
211
218
#### `getRootNode(options?)` {/*getrootnode*/}
212
219
213
220
Returns the root node containing the Fragment's parent DOM node, matching the behavior of [`Node.getRootNode()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode).
Compares the document position of the Fragment with another node, returning a bitmask matching the behavior of [`Node.compareDocumentPosition()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition).
@@ -240,6 +249,8 @@ const position = fragmentRef.current.compareDocumentPosition(otherElement);
240
249
241
250
A bitmask of [position flags](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition#return_value). Empty Fragments and Fragments with children rendered through a [portal](/reference/react-dom/createPortal) include `Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC` in the result.
* Methods that target children (such as `addEventListener`, `observeUsing`, and `getClientRects`) operate on *first-level host (DOM) children* of the Fragment. They do not directly target children nested inside another DOM element.
0 commit comments