Giter Club home page Giter Club logo

Comments (12)

ArnaudBarre avatar ArnaudBarre commented on June 20, 2024 1

Yeah true that CONSTANT should not get a warning, I need to improve this.
I think it's better to have the warning on Example than ExampleNode3.

from eslint-plugin-react-refresh.

ArnaudBarre avatar ArnaudBarre commented on June 20, 2024

This is a valid warning. Because of the object export the Example component cannot be fast refresh and the update will propagate to all importers. This should be detected by the build tool. What tool are you using to bundle React in dev?

from eslint-plugin-react-refresh.

eddie-dunn avatar eddie-dunn commented on June 20, 2024

This is a valid warning. Because of the object export the Example component cannot be fast refresh and the update will propagate to all importers.

Shouldn't the warning be on ExampleNode3 or perhaps only Example in that case?

CONSTANT should not get a warning, and the warning seems to disappear if I add a _ to the variable name.

This should be detected by the build tool. What tool are you using to bundle React in dev?

Vite.

from eslint-plugin-react-refresh.

kenlyon avatar kenlyon commented on June 20, 2024

I just discovered your plugin and am also trying to understand how it works. The part that confuses me is that the error is not on the thing being exported. If I had export const Foo = {} // Some big crazy thing. then I'd understand why it's got an error on it - it shouldn't be exported. But in the above example the Example function is not exported so why is it problematic?

I'm using a pattern copied from Material UI (MUI) where there are often some const variables declared in the .tsx file for things like class names and a styled() version of a component. If seems that those are considered problematic, even when not directly exported.

Is the problem more when a .tsx defines things other than components? Or is it because they're indirectly exported within the component?

from eslint-plugin-react-refresh.

ArnaudBarre avatar ArnaudBarre commented on June 20, 2024

The rule is "A file that contain at least one react component should not export something else than React components".

Why? Because to Fast Refresh components, the JS modules (i.e. the file) should be re-executed in isolation, and if something is exported, the importers need to get the latest reference without being re-executed. Fast Refresh take cares of 'providing the latest reference' for components exports, but not other exports.

Some framework like Remix can inject more code in development to provide this for specific exports, but this cannot be done reliably for random objects.

Note: In bundled env, this may not be needed as much as in Vite, I didn't work with Webpack in a long time.

from eslint-plugin-react-refresh.

eddie-dunn avatar eddie-dunn commented on June 20, 2024

Thanks for the explanation, @ArnaudBarre . I would suggest you put it in the README, to make it more n00b friendly :)

So, if I understand this correctly, the problem is if a module

  1. contains a 'component' (defined by some heuristic such as a function returning JSX, I assume?)
  2. exports a non-component

Is this correct?

from eslint-plugin-react-refresh.

ArnaudBarre avatar ArnaudBarre commented on June 20, 2024

Yeah that's it. Both are based on heuristic which were improved on 0.4.4.
The heuristic for 1 is components is mostly functions returning with starting with an uppercase or result of a memo/forwardRef call.
The heuristic for 2 is name startsWith lower case or is clearly a not a component (object, array)

For making the readme more clear this is in my todo. I hope I will have time to craft a page for the official doc (react.dev), Fast Refresh is missing some official doc.

from eslint-plugin-react-refresh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.