Course contentsShow
← React
Lesson 43 of 2,3962. Setup and Modern ToolingFree lesson

Hot Module Replacement (HMR)

Vite preserves component state during edits, making development feel instant without full page reloads.

Hot Module Replacement (HMR)

What you'll learn: How Vite keeps your React components alive during code changes without losing their state.

The Magic of Instant Updates

Remember when you had to refresh your entire page every time you changed a line of code? Your app would reset, you'd lose any data you entered, and you'd have to click through your UI all over again to test your changes.

Hot Module Replacement (HMR) solves this. When you edit a React component file and save it, Vite instantly updates just that component in your running app—without reloading the page or resetting your component's state.

How It Works in Practice

Imagine you're building a counter component. You've clicked the button 47 times during testing. Then you notice the button text needs to change from "Click me" to "Increment."

Without HMR: You save the file → browser refreshes → counter resets to 0 → you have to click 47 times again.

With HMR: You save the file → button text updates instantly → counter stays at 47.

Your component's state is preserved. The change appears immediately. Development feels like magic.

The Developer Experience Win

This isn't just about convenience—it's about workflow. When you're styling a modal that only appears after several user actions, HMR means you don't have to repeat those actions after every CSS tweak. When debugging a form with ten fields, you don't lose your test data on every code change.

Vite's HMR implementation works seamlessly with React because it understands component boundaries. It knows which parts can be swapped out without breaking your app's state.

Key Takeaway: HMR preserves your component state during edits, making development feel instant—you see changes immediately without losing your place in the app or restarting your testing workflow.