-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
HMR didn't work for dynamic imported modules (React) #20
Comments
You confirm that this is a Vite bug and not an upstream one? |
@davidbonnet it's quite arguable what's a We currently only have these two |
I have found the solution. I have updated the repository blocking version on package.json to reproduce the solution with the exact version of all packages of the initial issue. This is the fix: Really strange bug, seems that the dynamically imported component must be created and default exported separately. |
As I suspected, this an upstream issue located in For now, the issue raised by @Alex-Ferreli is intentional ("Anonymous direct exports […] are currently ignored."): ExportDefaultDeclaration(path) {
const node = path.node;
const decl = node.declaration;
const declPath = path.get('declaration');
if (decl.type !== 'CallExpression') {
// For now, we only support possible HOC calls here.
// Named function declarations are handled in FunctionDeclaration.
// Anonymous direct exports like export default function() {}
// are currently ignored.
return;
} Ironically, something like: function identity(value) {
return value;
}
export default identity(() => {
return <div>TRY TO CHANGE ME</div>;
}); …would work. It is however considered as bad practice to export unnamed components. The issue I witnessed is slightly different, and still located in IMHO, we can close this and re-open a new issue to upgrade the dependency once these are fixed. |
Thanks for digging into this @davidbonnet, I think we can leave this one open with the |
Opened facebook/react#21181 to that effect. |
Thanks @davidbonnet for the clarification. Maybe it's better to write it on the documentation, because this is the first time that i found and use this workaround, it's not specified anywhere. |
IMHO, it should at least refresh the page. I didn't have time yet to investigate the origin of that issue: |
I have same issue |
Not a bug. see vitejs/vite#4298. |
Same issue, any progress for it? |
In order to fix hot reloading, say you have a React component: export default () => <div /> You need to assign this to a named constant first, i.e. const MyComponent () => <div />
export default MyComponent |
I confirmed this still does not work. (Vite 4.0.2 + plugin-react 3.0.0) HMR not happening is expected due to the limitation of react refresh, but the full reload should happen. |
Do not export the component as anonymous function, assign it to a variable first, like written in this comment: |
This issue will be fixed by #79. Waiting for Vite 4.1 to enable this change Reminder: This is not a reason to use anonymous React component. This makes stack trace and React debugger pretty hard to use and is largely advise against by the React core team. Thanks a lot @sapphi-red for all the triage you did last month, it was very helpful! |
Describe the bug
Plugin-react-refresh didn't refresh dynamic imported modules.
Reproduction
Repository to reproduce:
https://github.com/Alex-Ferreli/vite-react-hmr-dynamic-import
It's the default
react-ts
template with a dynamic import of a React component.System Info
Output of
npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers
:Used package manager:
yarn
Logs
The HMR in console print the parent file:
[vite] hot updated: /src/App.tsx
Before submitting the issue, please make sure you do the following
The text was updated successfully, but these errors were encountered: