Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible false-positive with memo default export #27

Closed
silverwind opened this issue Oct 9, 2023 · 4 comments
Closed

Possible false-positive with memo default export #27

silverwind opened this issue Oct 9, 2023 · 4 comments

Comments

@silverwind
Copy link

silverwind commented Oct 9, 2023

import {memo} from "react";

export default memo(function Foo() {
  return <div/>;
})

Results in

  4:16  error  Fast refresh can't handle anonymous components. Add a name to your export  react-refresh/only-export-components

As far as I'm aware this is still a named component because of the function name and the module is asking for something impossible because a default export can not be named.

(The reason this can not be changed to a named export is because lazy requires a default export).

@silverwind silverwind changed the title Possible false-positive with memoed default export Possible false-positive with memo default export Oct 9, 2023
@ArnaudBarre
Copy link
Owner

ArnaudBarre commented Oct 9, 2023

The solution is to write:

import {memo} from "react";

const MemoFoo = memo(function Foo() {
  return <div/>;
});
MemoFoo.displayName = "MemoFoo";
export default MemoFoo;

This is a bot verbose I agree but it's more imposed by the React team to improve naming inside the react devtool and stacktraces.
I need to verify the difference in reality, and maybe it could be optional for some cases because this pattern actually fast-refresh correctly at runtime.

@silverwind
Copy link
Author

silverwind commented Oct 9, 2023

As far as I'm aware in regards to DevTools, displayName is not necessary if it's a named function statement like above because DevTools can just access function.prototype.name.

The export default with the variable should work as far as the linter is concerned, thought I don't particularily like this verbosity :)

@ArnaudBarre
Copy link
Owner

Fixed, will released later this week when other issues are handled!

@ArnaudBarre
Copy link
Owner

Released in v0.4.4!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants