Socket
Socket
Sign inDemoInstall

fix-react-refresh-plugin

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fix-react-refresh-plugin

This Vite plugin helps overcome a problem of not reloading some React components after saving changes.


Version published
Weekly downloads
155
decreased by-5.49%
Maintainers
1
Weekly downloads
 
Created
Source

fix-react-refresh-plugin

This Vite plugin helps overcome a problem of not reloading some React components after saving changes.

Note that this plugin does not fix this issue, but only applies a workaround. Before use, check whether the problem has been resolved at the level of the modules responsible for HMR.

Background

If you are using lazily loaded class components additionally wrapped in HOCs then HMR problem can occur. We faced these types of issues when moving our app from Create React App to Vite. The foundation of our source code was created many years ago and we still use class components in many places and cannot easily rewrite them. While debugging, we noticed that if we add a functional component to a class component module, then reloading works. Here is an example:

🔴 This code does not work.

import { lazy, Suspense, Component } from 'react';

const Tool = lazy(() => import('./Tool');

class MyComponent extends Component {
  render() {
    return (
      <div>
        {someCondition && (
          <Suspense>
            <Tool />
          </Suspense>
        )}
      </div>
    );
  }
);

export default withHoc(MyComponent);

🟢 This code does work.

import { lazy, Suspense, Component } from 'react';

const Tool = lazy(() => import('./Tool');

const NoopFC = () => null; // Here is an empty functional component.

class MyComponent extends Component {
  render() {
    return (
      <div>
        {someCondition && (
          <Suspense>
            <Tool />
          </Suspense>
        )}
      </div>
    );
  }
);

export default withHoc(MyComponent);

Therefore, we came up with an idea for a plugin that will automatically add an empty functional component to class component modules to force refreshing. If you need more information about this error and alternative ways to resolve it, check out this issue.

Usage

Install the plugin via your package manager:

npm i -D fix-react-refresh-plugin

Import and apply it to your vite.config.js file:

import fixReactRefresh from 'fix-react-refresh-plugin';

export default defineConfig({
  plugins: [
    {
      ...fixReactRefresh(),
      enforce: 'pre',
    },
    react()
  ]
});

⚠️ fixReactRefresh() must be set before react() and must have enforce set to pre.

Configuration

The plugin is prepared to work right out of the box, but for more sophisticated cases you may need to change the configuration. Below is a list of configurable options:

Property nameDescriptionTypeDefault valueRequired
componentFileNameRegExMatch the pattern of React component files.RegExp/\.(jsx|tsx)/No
classComponentDeclarationRegExMatch the pattern of React class components declarations.RegExp/(export(\sdefault)*)*\s*class\s+\w+\s+extends\s+(React.(Component|PureComponent)|(Component|PureComponent))/gmNo
noopFunctionalComponentNameThe name of the added noop function that forces component reloading. You must use the PascalCase notation and avoid special characters.stringFixReactRefreshFCNo

Keywords

FAQs

Package last updated on 04 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc