eslint-plugin-react: react/display-name false positive when wrapping inline function with React.memo
In 7.12.3, react/display-name gives a false positive if you wrap a function directly.
import React from 'react';
const Test = React.memo(props => (
<div>{props.children}</div>
));
export default Test;
Related: (#2105 #2109)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 15 (10 by maintainers)
@ThiefMaster simple solution there, use a default export š
Haha, donāt worry, we also donāt use string selectors š So yeah it sounds like the main reason not to use them is the sneakiness of not getting a name?
I just like keeping my eyes on ābest practicesā š Our situation here was we tried:
which violated
eslint(react/display-name). We went with:before we saw this thread. So now our options seem to be:
is that right? Or is there another option Iām missing for exporting a memoized component with a display name? Would the preference be for the fourth option here?
unfortunately having to use a named function here is super ugly because it means i need to specify the function name twice when using a non-default export
for default exports i can do this to avoid it, and still have a proper name for the component (since itās inferred automatically):
In latest build, this appears to be broken for a default export (although⦠Iām in a typescript project, so this might be a problem specific to linting .tsx files):
triggers react/display-name, but:
does not.
Indeed, in that case you have to store it in a variable first, or:
You shouldnāt be using enzyme string selectors in any case š debugging is harder without a name, and arrow functions only have a name via inference, which often will surprise you and not result in any name being inferred.
(You should allow default exports, but)
export function Foo() {}works just fine and is what Iād recommend in that case.If you use a named function instead of an arrow function (as is best practice for components, always) does the rule pass?
Yes, I would suggest 3 or 4. Note that 4 allows you to more easily put
propTypeson the component directly, which you should always do (and which this plugin will warn you about also)