ArnaudBarre / eslint-plugin-react-refresh

Validate that your components can safely be updated with Fast Refresh
MIT License
220 stars 15 forks source link

Possible false-positive with memo default export #44

Closed SukkaW closed 4 months ago

SukkaW commented 4 months ago

See also #27.

Example code:

import { memo } from 'react';

const Component = () => {
  return <div />;
};

console.log(Component.name);
// >> "Component"
// So actually `Component` is also named

export default memo(Component);
// eslint.config.js
import eslint from '@eslint/js';
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint"

export default tseslint.config(
  eslint.configs.recommended,
  {
    files: ['src/**/*.tsx'],
    // in main config for TSX/JSX source files
    plugins: {
      "react-refresh": reactRefresh,
    },
    rules: {
      "react-refresh/only-export-components": "warn",
    },
  },
  ...tseslint.configs.recommended,
);

And minimum reproduction on ESLint REPL.

ArnaudBarre commented 4 months ago

Yeah this pattern should be allowed to

SukkaW commented 4 months ago

@ArnaudBarre I've created a PR #45 to fix this issue.

sparrowek commented 3 months ago

A minor problem is that the PR does not handle React.memo() only memo() correctly

ArnaudBarre commented 3 months ago

ah yeah I never use this pattern, I still hope that one day React will be ESM 😂 but should be supported anyway, I'll do a quick fix

ptmkenny commented 3 months ago

When will this fix be available? I just tried out this plugin today and was stumped why I was getting errors with

export default React.memo(MyComponent);

Fast refresh only works when a file only exports components. Move your component(s) to a separate file. Fast refresh can't handle anonymous components. Add a name to your export.

ArnaudBarre commented 3 months ago

Sorry I though I'll do it directly and forgot about it! Available in 0.4.10 thanks to @SukkaW!

ptmkenny commented 3 months ago

Thanks to both of you!