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
+24-36Lines changed: 24 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: <Fragment> (<>...</>)
6
6
7
7
`<Fragment>`, often used via `<>...</>` syntax, lets you group elements without a wrapper node.
8
8
9
-
<ExperimentalBadge /> Fragments can also accept refs, which enable interacting with underlying DOM nodes without adding wrapper elements.
9
+
<Experimental> Fragments can also accept refs, which enable interacting with underlying DOM nodes without adding wrapper elements. See reference and usage below.</Experimental>
10
10
11
11
```js
12
12
<>
@@ -27,43 +27,37 @@ title: <Fragment> (<>...</>)
27
27
28
28
Wrap elements in `<Fragment>` to group them together in situations where you need a single element. Grouping elements in `Fragment` has no effect on the resulting DOM; it is the same as if the elements were not grouped. The empty JSX tag `<></>` is shorthand for `<Fragment></Fragment>` in most cases.
29
29
30
-
<ExperimentalBadge /> Fragments can accept refs to provide access to the underlying DOM nodes they wrap, enabling interactions like event handling, intersection observation, and focus management without requiring additional wrapper elements.
31
-
32
-
#### Props {/*props*/}
33
-
34
-
-**optional**`key`: Fragments declared with the explicit `<Fragment>` syntax may have [keys.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
35
-
- <ExperimentalBadge /> **optional**`ref`: A ref object or callback function. React will forward the ref to a `FragmentInstance` object that provides methods for interacting with the DOM nodes wrapped by the Fragment.
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:
41
33
42
34
**Event handling methods:**
43
-
-`addEventListener(type, listener, options?)`: Adds an event listener to all first-level DOM children of the fragment
44
-
-`removeEventListener(type, listener, options?)`: Removes an event listener from all first-level DOM children of the fragment
35
+
-`addEventListener(type, listener, options?)`: Adds an event listener to all first-level DOM children of the Fragment
36
+
-`removeEventListener(type, listener, options?)`: Removes an event listener from all first-level DOM children of the Fragment
45
37
-`dispatchEvent(event)`: Dispatches an event to a virtual child of the Fragment to call any added listeners and bubble to the DOM parent.
46
38
47
39
**Layout methods:**
48
-
-`compareDocumentPosition(otherNode)`: Compares the document position of the fragment with another node
49
-
- An element preceding a fragment is `Node.DOCUMENT_POSITION_PRECEDING`
50
-
- An element following a fragment is `Node.DOCUMENT_POSITION_FOLLOWING`
51
-
- An element containing the fragment is `Node.DOCUMENT_POSITION_PRECEDING` and `Node.DOCUMENT_POSITION_CONTAINING`
52
-
- An element within the fragment is `NODE.DOCUMENT_POSITION_CONTAINED_BY`
40
+
-`compareDocumentPosition(otherNode)`: Compares the document position of the Fragment with another node
41
+
- If the Fragment has children, the native `compareDocumentPosition` value is returned.
53
42
- Empty Fragments will attempt to compare positioning within the React tree and include `NODE.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`
54
43
- 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`
55
44
-`getClientRects()`: Returns a flat array of `ClientRect` objects representing the bounding rectangles of all DOM nodes
56
-
-`getRootNode()`: Returns the root node containing the fragment's DOM nodes
45
+
-`getRootNode()`: Returns the root node containing the Fragment's DOM nodes
57
46
58
47
**Focus management methods:**
59
48
-`focus(options?)`: Focuses the first focusable DOM node in the Fragment
60
49
-`focusLast(options?)`: Focuses the last focusable DOM node in the Fragment
61
-
-`blur()`: Removes focus from all DOM nodes in the Fragment
50
+
-`blur()`: Removes focus if `document.activeElement` is within the Fragment
62
51
63
52
**Observer methods:**
64
-
-`observeUsing(observer)`: Starts observing the fragment's DOM children with an IntersectionObserver or ResizeObserver
65
-
-`unobserveUsing(observer)`: Stops observing the fragment's DOM children with the specified observer
66
-
</Experimental>
53
+
-`observeUsing(observer)`: Starts observing the Fragment's DOM children with an IntersectionObserver or ResizeObserver
54
+
-`unobserveUsing(observer)`: Stops observing the Fragment's DOM children with the specified observer
55
+
56
+
57
+
#### Props {/*props*/}
58
+
59
+
-**optional**`key`: Fragments declared with the explicit `<Fragment>` syntax may have [keys.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
60
+
- <ExperimentalBadge /> **optional**`ref`: A ref object or callback function. React will forward the ref to a `FragmentInstance` object that provides methods for interacting with the DOM nodes wrapped by the Fragment.
67
61
68
62
#### Caveats {/*caveats*/}
69
63
@@ -247,10 +241,9 @@ function PostBody({ body }) {
247
241
248
242
</Sandpack>
249
243
250
-
<Experimental>
251
244
---
252
245
253
-
### Using Fragment refs for DOM interaction {/*using-fragment-refs-for-dom-interaction*/}
246
+
### <ExperimentalBadge /> Using Fragment refs for DOM interaction {/*using-fragment-refs-for-dom-interaction*/}
254
247
255
248
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()`.
256
249
@@ -278,7 +271,7 @@ function ClickableWrapper({ children, onClick }) {
278
271
```
279
272
---
280
273
281
-
### Tracking visibility with Fragment refs {/*tracking-visibility-with-fragment-refs*/}
274
+
### <ExperimentalBadge /> Tracking visibility with Fragment refs {/*tracking-visibility-with-fragment-refs*/}
282
275
283
276
Fragment refs are particularly useful for visibility tracking and intersection observation. This enables you to monitor when content becomes visible without requiring the child components to expose refs:
284
277
@@ -291,7 +284,7 @@ function VisibilityObserver({ threshold = 0.5, onVisibilityChange, children }) {
0 commit comments