React Transform HMR
react-transform-hmr is a Babel transform designed to enable hot module reloading (HMR) for React components, primarily leveraging Webpack's Hot Module Replacement API. Currently at version 1.0.4, its development has seen minor updates, mainly around `react-proxy` bumps. It is explicitly presented as "highly experimental tech" and "not long term," intended more for prototyping future React developer experiences than for stable project use. The package's explicit goal is to be superseded by less hacky, built-in React features. It requires `babel-plugin-react-transform` as a prerequisite and operates by modifying React class components at compile-time to support HMR. A significant limitation is its inability to work with stateless functional components introduced in React 0.14 and later. Users are strongly cautioned against relying on it for production or long-term projects, as the underlying technologies could drastically change or be deprecated. Its release cadence was active in late 2015, but has since ceased, indicating an abandoned status in favor of more stable HMR solutions like React Refresh.
Common errors
-
Uncaught SyntaxError: Unexpected reserved word
cause Using `.babelrc` to configure `react-transform-hmr` in a React Native project.fixConfigure `react-transform-hmr` directly via `babel-loader` options in your `webpack.config.js` for React Native, rather than using a separate `.babelrc` file.
Warnings
- breaking Starting from v1.0.0, the transform no longer acts as a no-op in production environments. It is now your responsibility to ensure it's only enabled in development.
- deprecated This technology is explicitly labeled as 'highly experimental' and 'not long term', intended for prototyping rather than stable projects. The goal is to eventually replace it with less hacky, built-in React features.
- gotcha This transform does not currently work for stateless functional components, which were introduced in React 0.14.
- gotcha When using with React Native, configuring `react-transform-hmr` via `.babelrc` can lead to issues such as `Uncaught SyntaxError: Unexpected reserved word` or changes not being picked up by the packager's aggressive caching.
- gotcha You should not use `react-transform-hmr` concurrently with React Hot Loader, as it's not needed and can cause conflicts.
Install
-
npm install react-transform-hmr -
yarn add react-transform-hmr -
pnpm add react-transform-hmr
Imports
- react-transform-hmr
import ReactTransformHMR from 'react-transform-hmr'; const ReactTransformHMR = require('react-transform-hmr');This is a Babel transform configured in .babelrc, not directly imported into JS/TS files. It is referenced as a string within the 'transforms' array.
Quickstart
{ "presets": ["es2015", "stage-0"],
"env": {
"development": {
"plugins": [["babel-plugin-react-transform", {
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}]
}]]
}
}
}