Skip to content

jsx-key rule should error on returning short hand fragments (<>) from iterators to avoid false negative  #3657

@GNRSN

Description

@GNRSN

Issue

Mapping a list into multiple components per list element, surrounding them with jsx shorthand fragments <> currently results in no eslint error with jsx-key rule enabled, even though React will warn/error about missing keys.

According to the documentation, the recommendation is to instead always return full fragments from iterators, i.e.

<Fragment key={key}>
   ...
</Fragment>

See the short section in: https://react.dev/reference/react/Fragment#rendering-a-list-of-fragments

Suggestion

With the jsx-key rule enabled, shorthand fragments returned from an iterator should still error without key, fix could update to <React.Fragment key={key}>

Motivation

Since missing keys can be pretty hard to debug, especially on fragments which won't show up in devtools, having eslint help with this would be extremely valuable instead of having to debug once it hits staging.

Misc

#2365 seems to be the most recent issue on this topic, discussing the removal of key requirement for shorthand fragments as they don't support it

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions