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

False positives for `no-this-in-sfc` for functions returning null

Open chiawendt opened this issue 3 years ago • 4 comments

In the following cases the functions should not be considered a component, if they returns only null without jsx.

https://github.com/jsx-eslint/eslint-plugin-react/blob/4b4bba91ddebe27cf6bdd9f86d2b9a824a07a8fb/tests/lib/rules/no-this-in-sfc.js#L245-L255

https://github.com/jsx-eslint/eslint-plugin-react/blob/4b4bba91ddebe27cf6bdd9f86d2b9a824a07a8fb/tests/lib/rules/no-this-in-sfc.js#L278-L290

https://github.com/jsx-eslint/eslint-plugin-react/blob/4b4bba91ddebe27cf6bdd9f86d2b9a824a07a8fb/tests/lib/rules/no-this-in-sfc.js#L291-L302

chiawendt avatar Apr 23 '22 15:04 chiawendt

Yes, they should - components don't have to return jsx, they can return null, numbers, strings, or arrays of such (and in react 18+, undefined).

ljharb avatar Apr 23 '22 15:04 ljharb

I got you. But do you think these test cases should be reported? If these nulls are replaced with numbers, strings, or arrays, should they be reported?

chiawendt avatar Apr 23 '22 16:04 chiawendt

There is a distinction between what React engine considered a component, and what a developer considered a component. For a function returning only null or numbers, more likely than not, developers do not considered it a component.

chiawendt avatar Apr 23 '22 17:04 chiawendt

More likely than not, but it’s sometimes better to have false positives than to miss a component.

Components that only return null are far more common than any other kind of non-jsx-returning component, I’m pretty sure, so i think it’s fine to catch the former and not the latter.

ljharb avatar Apr 23 '22 22:04 ljharb

I think this is resolved, but please file a new issue if not.

ljharb avatar Sep 30 '22 03:09 ljharb