-
Notifications
You must be signed in to change notification settings - Fork 13
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
False positives in v0.4.4 #30
Comments
This is a valid warning. Because of the object export the |
Shouldn't the warning be on
Vite. |
Yeah true that |
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 I'm using a pattern copied from Material UI (MUI) where there are often some Is the problem more when a |
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. |
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
Is this correct? |
Yeah that's it. Both are based on heuristic which were improved on 0.4.4. 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. |
Hey @ArnaudBarre , any updates on the
|
Can you provide an example of file where this triggers? Here the warning is not totally wrong, as soon as you have a component inside a file that export something else you need to change something |
@ArnaudBarre , looks like the error is a bit different, but basically here is situation: This will cause warning, eslint points to first line: const MyConstant = 1234;
export default function doSomething() {
console.log(MyConstant);
} but this one is fine (though shouldn't be, because file is jsx, but it's not a component) const myConstant = 1234;
export default function doSomething() {
console.log(myConstant);
} this one is also fine: const MY_CONSTANT = 1234;
export default function doSomething() {
console.log(MY_CONSTANT);
} |
Hum I confused can you provide me an example of a JSX file that actually contains JSX and create a warning you think is not correct?
|
I think I got it: This will work fine: const MyConstant = 1234;
export default function MyComponent() {
return <div>{MyConstant}</div>;
} changing function name to lower case will cause warning but that's expected behavior: const MyConstant = 1234;
export default function myComponent() {
return <div>{MyConstant}</div>;
} However the misleading part is that eslint complains about first line. Now const myConstant = 1234;
export default function myComponent() {
return <div>{myConstant}</div>;
} Will not cause any warnings. Is that expected? |
I'm getting the following error
Fast refresh only works when a file only exports components. Move your component(s) to a separate file
in a lot of.tsx
files which only have one export.Example code for triggering it:
Result
Expected result:
CONSTANT
.No warning forOnly a warning forExample
.Example
The text was updated successfully, but these errors were encountered: