What is react-hot-loader?
react-hot-loader is a tool that allows React components to be live reloaded without losing their state. This is particularly useful during development as it speeds up the feedback loop by allowing developers to see changes in real-time without refreshing the entire application.
What are react-hot-loader's main functionalities?
Hot Module Replacement
This feature allows you to wrap your root component with the `hot` function from react-hot-loader. This enables Hot Module Replacement (HMR) for the component, meaning that changes to the component will be applied in real-time without a full reload.
import { hot } from 'react-hot-loader/root';
import App from './App';
const HotApp = hot(App);
export default HotApp;
Preserve Component State
By using react-hot-loader, the state of your React components is preserved even when the component code is updated. This is particularly useful for maintaining the current state of your application during development.
import { hot } from 'react-hot-loader/root';
import App from './App';
const HotApp = hot(App);
export default HotApp;
Error Handling
react-hot-loader provides better error handling during development. When an error occurs, it does not break the entire application but instead shows the error in the console, allowing you to fix it without losing the application state.
import { hot } from 'react-hot-loader/root';
import App from './App';
const HotApp = hot(App);
export default HotApp;
Other packages similar to react-hot-loader
react-refresh
react-refresh is a more modern alternative to react-hot-loader, developed by the React team. It provides a more integrated and reliable hot reloading experience by leveraging React's new Fast Refresh feature. It is recommended for use with Create React App and other modern React setups.
webpack-hot-middleware
webpack-hot-middleware is a middleware for Webpack that enables hot module replacement (HMR) for any JavaScript application. While it is not specific to React, it can be used in conjunction with React to achieve similar hot reloading capabilities.
react-hot-toast
react-hot-toast is a lightweight package for showing toast notifications in React applications. While it does not provide hot reloading, it is often used in development environments to display real-time notifications, which can complement the hot reloading experience.
A Big Update Is Coming
React Hot Loader 3 is on the horizon, and you can try it today (boilerplate branch, upgrade example). It fixes some long-standing issues with both React Hot Loader and React Transform, and is intended as a replacement for both. The docs are not there yet, but they will be added before the final release. For now, this commit is a good reference.
React Hot Loader
This is a stable for daily use in development implementation of React live code editing.
The Talk
React Hot Loader was demoed together with Redux at React Europe.
Watch Dan Abramov's talk on Hot Reloading with Time Travel.
Installation
npm install --save-dev react-hot-loader
Usage
If you want to try hot reloading in a new project, try one of the starter kits, React Hot Boilerplate being the most minimal one.
To use React Hot Loader in an existing project, you need to
- switch to Webpack for builds (instead of RequireJS or Browserify);
- enable Hot Module Replacement, which is a Webpack feature;
- configure Webpack to use React Hot Loader for JS or JSX files.
These steps are covered by the walkthrough.
If you'd rather stay with Browserify, check out LiveReactload by Matti Lankinen.
Flux
Redux is a Flux implementation that supports hot reloading of everything out of the box. Read The Evolution of Flux Frameworks for some context around its creation.
React Native
React Native supports hot reloading natively as of version 0.22.
Troubleshooting
If something doesn't work, in 99% cases it's a configuration issue. A missing option, a wrong path or port. Webpack is very strict about configuration, and the best way to find out what's wrong is to compare your project to an already working setup, such as React Hot Boilerplate, bit by bit. We're also gathering Troubleshooting Recipes so send a PR if you have a lesson to share!
Documentation
Docs are in a bit of a flux right now because I'm in the process of updating everything to document the major 1.0 release.
If you just learned about React Hot Loader and want to find out more, check out the walkthrough and then try one of the starter kits.
If you've been with us for a while, read 1.0 release notes and migration guide.
Got Questions?
Watch the repo to stay tuned!
Patrons
The work on React Hot Loader, React Transform, Redux, and related projects was funded by the community.
Meet some of the outstanding companies that made it possible:
See the full list of React Hot Loader patrons.
License
MIT (https://opensource.org/licenses/mit-license.php)