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

Refresh does not always work with class components #199

Closed
n9 opened this issue Apr 26, 2024 · 0 comments · Fixed by #203
Closed

Refresh does not always work with class components #199

n9 opened this issue Apr 26, 2024 · 0 comments · Fixed by #203
Labels
bug Something isn't working

Comments

@n9
Copy link

n9 commented Apr 26, 2024

When it works

If a class component is exported from a module that also exports a functional component.

Sample:

import React from "react";

export class Bar1 extends React.Component {
    render() {
        return <span>Barx</span>
    }
}

export function DummyBarFc() {
    return <span>dummy</span>
}

When it does not work

If a class component is exported from a module that exports no functional component.

Sample:

import React from "react";

export class Bar1 extends React.Component {
    render() {
        return <span>Barx</span>
    }
}

// export function DummyBarFc() {
//     return <span>dummy</span>
// }

Why it does not work

The refresh runtime is only included

RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
import.meta.hot.accept((nextExports) => {
if (!nextExports) return;
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate("${id}", currentExports, nextExports);
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
});
});

if the code of module matches

const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;

However, the pattern is not generated by SWC for class components, as fast refresh does not preserve their state (see swc-project/swc#4431 (comment)).
So a module with no exported functional component is not handled by HMR.

(Note: I will provide a PR.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants