Restoring Fast Refresh when using React.lazy
Fast Refresh can't properly handle React.lazy
.
Replace dynamic imports to the regular imports
- restored developer experience with Fast Refresh
- can catch syntax errors instantly(in comparison, using "dynamic import" the code executes only when it loaded)
- use only in development environment
- import source must be a regular string
Usage with CRA and customize-cra
-
Enable Fast Refresh:
-
Using customize-cra-react-refresh addon
-
Coming soon, but didn't work yet. Using CRA env variable FAST_REFRESH. Read more here
-
-
Install the plugin
npm install --save-dev babel-plugin-transform-react-lazy
- Use the plugin
// config-overrides.js
const { override, addBabelPlugin } = require("customize-cra");
const { addReactRefresh } = require("customize-cra-react-refresh");
module.exports = override(
addReactRefresh(),
process.env.NODE_ENV === "development" &&
addBabelPlugin("transform-react-lazy")
);