React Hot Loader

4.13.1 · maintenance · verified Sun Apr 19

React Hot Loader is a utility that enables Hot Module Replacement (HMR) for React components, allowing developers to tweak components in real time without losing application state during development. The current stable version is 4.13.1, which receives frequent bug fixes and minor updates, as evidenced by its release history. It works by patching React's reconciliation process to allow components to be swapped out without re-mounting, preserving component state and improving development efficiency. While it has been a cornerstone for many React development workflows, it is explicitly positioned as being superseded by React Fast Refresh. Users are strongly encouraged to migrate to Fast Refresh if their environment (e.g., React Native, Parcel 2, Webpack with a plugin, Create React App v4+, Next.js v9.4+) supports it, as Fast Refresh offers a more integrated and robust solution. However, React Hot Loader remains a viable option for environments that do not yet support Fast Refresh.

Common errors

Warnings

Install

Imports

Quickstart

Sets up React Hot Loader with Babel and Webpack, demonstrates basic HMR for a root React component and showcases a reloadable React Hook.

// .babelrc
{
  "plugins": ["react-hot-loader/babel"]
}

// webpack.config.js
module.exports = {
  mode: 'development',
  entry: ['react-hot-loader/patch', './src/index.tsx'],
  // ... other webpack config
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      }
    ]
  },
  resolve: {
    alias: {
      'react-dom': '@hot-loader/react-dom' // For hooks support
    },
    extensions: ['.tsx', '.ts', '.js']
  }
};

// src/index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { hot } from 'react-hot-loader/root';

const App = () => {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    // This hook will reload because its dependency list is non-empty
    console.log('App component mounted or count changed:', count);
  }, [count, "hot"]); // Adding "hot" ensures reloadability

  return (
    <div>
      <h1>Hello React Hot Loader!</h1>
      <p>Count: {count}</p>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
      <p>Edit this component and save to see HMR in action.</p>
    </div>
  );
};

const HotApp = hot(App);

ReactDOM.render(<HotApp />, document.getElementById('root'));

view raw JSON →