eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

`jsx-fragments`: allow `Fragment` syntax option

Open nicmosc opened this issue 1 year ago • 1 comments

Description

Right now one can specify either syntax or element, however element automatically falls back to using React.Fragment which is not supported in the case the variable is not exposed as an UMD global - the case when using es modules.

This option allows to use modules instead and import the Fragment component from the react library

Note: we need this behaviour for our team so we've patched the library with the same changes you see here for now.

Usage

'react/jsx-fragments': ['warn', 'elementShort'],

Showcase

https://github.com/user-attachments/assets/bc2a1ee7-9b76-4220-ae07-d4a39d87b645

Open to:

  • using a different option name instead of elementShort e.g. module?
  • if there is a way to achieve this without modifying the rule OR there is a way to infer this from the project settings it's probably better - any ideas?

nicmosc avatar Sep 02 '24 11:09 nicmosc

Historically, you’re required to have React in scope for jsx to work, so this never came up until the new jsx renderer in React 17.

However, in a modern codebase, why wouldn’t you always prefer the syntax?

ljharb avatar Sep 02 '24 15:09 ljharb