no-useless-fragment
Full Name in eslint-plugin-react-x
Full Name in @eslint-react/eslint-plugin
Features
🔍
🔧
⚙️
Presets
core
recommended
recommended-typescript
recommended-type-checked
What it does
Prevents the use of useless fragments.
A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment.
Moreover, rendering fragments with multiple levels of depth can cause React to not retain the state of components nested within the fragment after certain re-renders, which may lead to unexpected state resetting.
Examples
Failing
Passing
Note
By default, this rule always allows single expressions in a fragment. This is useful in
places like Typescript where string
does not satisfy the expected return type
of JSX.Element
. A common workaround is to wrap the variable holding a string
in a fragment and expression. To change this behaviour, use the allowExpressions
option.