Fragment

The fragment component allows you to group multiple elements without adding extra nodes to the DOM. This is especially useful when you need to return several elements but want to avoid wrapping them in an additional element. By using fragment, you can maintain a clean DOM tree and prevent unnecessary nesting.

Example

Fragment Basic Example

Rendering a List

When rendering multiple elements in a loop, ensure each fragment has a unique key. This is crucial if array items might be inserted, deleted, or reordered.

API Reference

A React.Fragment: https://react.dev/reference/react/Fragment. Used to group elements together without a wrapper node.

Returns: Element The rendered fragment element.

ParametersTypeDefaultDescription
*childrenAnyThe children in the fragment.
keystr |
None
NoneA unique identifier used by React to render elements in a list.