unnecessaryFragments
Disallow unnecessary JSX fragments that wrap a single child or have no children.
✅ This rule is included in the jsx stylistic presets.
JSX fragments (<>...</> or <Fragment>...</Fragment>) are used to group multiple elements without adding extra nodes to the DOM.
However, when a fragment wraps only a single child or has no children at all, it serves no purpose and adds unnecessary complexity to the code.
Examples
Section titled “Examples”const const element: JSX.Element
element = ( <> <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>Hello</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> </>);const const element: JSX.Element
element = <>Hello</>;const const element: JSX.Element
element = <></>;const const element: JSX.Element
element = ( <const Fragment: any
Fragment> <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>Hello</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> </const Fragment: any
Fragment>);const const element: JSX.Element
element = <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>Hello</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>;const const element: "Hello"
element = "Hello";const const element: JSX.Element
element = ( <> <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>First</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>Second</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> </>);const const element: JSX.Element
element = ( <const Fragment: any
Fragment Attributes.key?: Key | null | undefined
key="item"> <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>Hello</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> </const Fragment: any
Fragment>);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase has specific requirements where fragments are used consistently for stylistic reasons, you may want to disable this rule. However, removing unnecessary fragments is generally recommended to keep code concise and maintainable.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Deno:
jsx-no-useless-fragment - ESLint:
react/jsx-no-useless-fragment
Made with ❤️🔥 around the world by
the Flint team and contributors.