Fragment
<Fragment>, often used via <>...</> syntax, lets you group elements without a wrapper node.
<>
<View />
<Text>Hello</Text>
</>
For full documentation on Fragment usage including grouping elements, assigning to variables, and rendering lists with keys, see the Fragment documentation on react.dev.
Fragment Refs in React Nativeβ
This API is currently only available in React Nativeβs Canary and Experimental channels.
If you want to try it out, please enable the Canary Channel in your app.
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:
import {Fragment, useRef} from 'react';
function MyComponent() {
const fragmentRef = useRef(null);
return (
<Fragment ref={fragmentRef}>
<View />
<Text>Hello</Text>
</Fragment>
);
}
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.
Reference
FragmentInstanceβ
The FragmentInstance provided by Fragment refs in React Native supports the following methods:
observeUsing(observer)β
Starts observing all first-level host children of the Fragment with the provided observer.
const observer = new IntersectionObserver(callback, options);
fragmentRef.current.observeUsing(observer);
Parametersβ
observer: AnIntersectionObserverinstance.
Returnsβ
undefined.
Unlike the Web implementation, React Native only supports IntersectionObserver with observeUsing. ResizeObserver is not supported.
unobserveUsing(observer)β
Stops observing the Fragment's host children with the specified observer.
fragmentRef.current.unobserveUsing(observer);
Parametersβ
observer: The sameIntersectionObserverinstance previously passed toobserveUsing.
Returnsβ
undefined.
getClientRects()β
Returns an array of bounding rectangles for all first-level host children of the Fragment.
const rects = fragmentRef.current.getClientRects();
Returnsβ
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.
getRootNode(options?)β
Returns the root node containing the Fragment's parent host node.
const root = fragmentRef.current.getRootNode();
Parametersβ
- optional
options: An object with acomposedboolean property, matching the DOMgetRootNodeAPI.
Returnsβ
A Node or the FragmentInstance itself if there is no parent host node.
compareDocumentPosition(otherNode)β
Compares the position of the Fragment with another node, returning a bitmask matching the behavior of Node.compareDocumentPosition().
const position =
fragmentRef.current.compareDocumentPosition(otherElement);
Parametersβ
otherNode: A host instance to compare against.
Returnsβ
A bitmask of position flags. Empty Fragments return Node.DOCUMENT_POSITION_DISCONNECTED.
reactFragments propertyβ
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.
Methods Not Available in React Nativeβ
The following FragmentInstance methods are available on Web but are not supported in React Native:
| Method | Description |
|---|---|
addEventListener | Adds an event listener to all first-level host children. |
removeEventListener | Removes an event listener from all first-level host children. |
dispatchEvent | Dispatches an event on the Fragment. |
focus | Focuses the first focusable node depth-first. |
focusLast | Focuses the last focusable node depth-first. |
blur | Removes focus from the active element if within the Fragment. |
scrollIntoView | Scrolls the Fragment's children into view. |
See the react.dev FragmentInstance documentation for details on these methods.
Caveatsβ
- Methods that target children (such as
observeUsingandgetClientRects) operate on first-level host children of the Fragment. They do not target children nested inside another host element. observeUsingdoes not work on text nodes.- You cannot use the
<>...</>shorthand syntax when passingreforkeyto a Fragment. You must explicitly importFragmentfrom'react'.