Skip to content

Commit 8b1545e

Browse files
committed
Add docs for RN fragment ref operations
1 parent 3b4bf1c commit 8b1545e

2 files changed

Lines changed: 168 additions & 0 deletions

File tree

docs/fragment.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
id: fragment
3+
title: Fragment
4+
---
5+
6+
import CanaryChannelAPIWarning from './\_canary-channel-api-warning.mdx';
7+
8+
`<Fragment>`, often used via `<>...</>` syntax, lets you group elements without a wrapper node.
9+
10+
```jsx
11+
<>
12+
<View />
13+
<Text>Hello</Text>
14+
</>
15+
```
16+
17+
For full documentation on `Fragment` usage including grouping elements, assigning to variables, and rendering lists with keys, see the [Fragment documentation on react.dev](https://react.dev/reference/react/Fragment).
18+
19+
## Fragment Refs in React Native
20+
21+
<CanaryChannelAPIWarning />
22+
23+
Fragment refs let you attach a `ref` to a `<Fragment>` and interact with its host children without adding wrapper views. To use a Fragment ref, you must use the explicit `<Fragment>` syntax:
24+
25+
```jsx
26+
import {Fragment, useRef} from 'react';
27+
28+
function MyComponent() {
29+
const fragmentRef = useRef(null);
30+
return (
31+
<Fragment ref={fragmentRef}>
32+
<View />
33+
<Text>Hello</Text>
34+
</Fragment>
35+
);
36+
}
37+
```
38+
39+
When you pass a `ref` to a Fragment, React provides a `FragmentInstance` object. The React Native `FragmentInstance` implements a subset of the methods available on Web. For the full `FragmentInstance` API including Web-only methods, see the [FragmentInstance documentation on react.dev](https://react.dev/reference/react/Fragment#fragmentinstance).
40+
41+
---
42+
43+
# Reference
44+
45+
## `FragmentInstance`
46+
47+
The `FragmentInstance` provided by Fragment refs in React Native supports the following methods:
48+
49+
### `observeUsing(observer)` {#observeusing}
50+
51+
Starts observing all first-level host children of the Fragment with the provided observer.
52+
53+
```jsx
54+
const observer = new IntersectionObserver(callback, options);
55+
fragmentRef.current.observeUsing(observer);
56+
```
57+
58+
#### Parameters
59+
60+
- `observer`: An [`IntersectionObserver`](global-intersectionobserver) instance.
61+
62+
#### Returns
63+
64+
`undefined`.
65+
66+
:::note
67+
Unlike the Web implementation, React Native only supports `IntersectionObserver` with `observeUsing`. `ResizeObserver` is not supported.
68+
:::
69+
70+
---
71+
72+
### `unobserveUsing(observer)` {#unobserveusing}
73+
74+
Stops observing the Fragment's host children with the specified observer.
75+
76+
```jsx
77+
fragmentRef.current.unobserveUsing(observer);
78+
```
79+
80+
#### Parameters
81+
82+
- `observer`: The same `IntersectionObserver` instance previously passed to [`observeUsing`](#observeusing).
83+
84+
#### Returns
85+
86+
`undefined`.
87+
88+
---
89+
90+
### `getClientRects()` {#getclientrects}
91+
92+
Returns an array of bounding rectangles for all first-level host children of the Fragment.
93+
94+
```jsx
95+
const rects = fragmentRef.current.getClientRects();
96+
```
97+
98+
#### Returns
99+
100+
An `Array<DOMRect>` containing the bounding rectangles of all first-level host children. In React Native, each rectangle is obtained via `getBoundingClientRect()` on the underlying host instance.
101+
102+
---
103+
104+
### `getRootNode(options?)` {#getrootnode}
105+
106+
Returns the root node containing the Fragment's parent host node.
107+
108+
```jsx
109+
const root = fragmentRef.current.getRootNode();
110+
```
111+
112+
#### Parameters
113+
114+
- **optional** `options`: An object with a `composed` boolean property, matching the [DOM `getRootNode` API](https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode#options).
115+
116+
#### Returns
117+
118+
A `Node` or the `FragmentInstance` itself if there is no parent host node.
119+
120+
---
121+
122+
### `compareDocumentPosition(otherNode)` {#comparedocumentposition}
123+
124+
Compares the 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).
125+
126+
```jsx
127+
const position =
128+
fragmentRef.current.compareDocumentPosition(otherElement);
129+
```
130+
131+
#### Parameters
132+
133+
- `otherNode`: A host instance to compare against.
134+
135+
#### Returns
136+
137+
A bitmask of [position flags](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition#return_value). Empty Fragments return `Node.DOCUMENT_POSITION_DISCONNECTED`.
138+
139+
---
140+
141+
### `reactFragments` property {#reactfragments}
142+
143+
Each first-level host child of a Fragment with a `ref` gets a `reactFragments` property — a `Set<FragmentInstance>` containing all Fragment instances that own the element.
144+
145+
---
146+
147+
## Methods Not Available in React Native
148+
149+
The following `FragmentInstance` methods are available on Web but are **not supported** in React Native:
150+
151+
| Method | Description |
152+
| ---------------------- | ------------------------------------------------------------------- |
153+
| `addEventListener` | Adds an event listener to all first-level host children. |
154+
| `removeEventListener` | Removes an event listener from all first-level host children. |
155+
| `dispatchEvent` | Dispatches an event on the Fragment. |
156+
| `focus` | Focuses the first focusable node depth-first. |
157+
| `focusLast` | Focuses the last focusable node depth-first. |
158+
| `blur` | Removes focus from the active element if within the Fragment. |
159+
| `scrollIntoView` | Scrolls the Fragment's children into view. |
160+
161+
See the [react.dev FragmentInstance documentation](https://react.dev/reference/react/Fragment#fragmentinstance) for details on these methods.
162+
163+
## Caveats
164+
165+
- Methods that target children (such as `observeUsing` and `getClientRects`) operate on **first-level host children** of the Fragment. They do not target children nested inside another host element.
166+
- `observeUsing` does not work on text nodes.
167+
- You cannot use the `<>...</>` shorthand syntax when passing `ref` or `key` to a Fragment. You must explicitly import `Fragment` from `'react'`.

website/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ export default {
300300
'activityindicator',
301301
'button',
302302
'flatlist',
303+
'fragment',
303304
'image',
304305
'imagebackground',
305306
'keyboardavoidingview',

0 commit comments

Comments
 (0)