APIs

Children.only

Returns the input when it is a single React element; throws for other inputs, including an array containing one element.

Formal Syntax & Structure

react syntaxreact
Children.only(children)

Parameters & Attributes

NameTypeDescription
childrenReact childrenOpaque children value.

Return Value

React element

The same single element.

Working Example

Example Snippetreact
import { Children, createElement } from 'react';const item = createElement('span', null, 'Ready');console.log(Children.only(item) === item);
React Reference