Overview
SAAC Frontend uses Vite’s Hot Module Replacement (HMR) with React Fast Refresh to provide instant feedback during development. Changes to your code appear in the browser without requiring a full page reload, preserving application state.How HMR Works
React Fast Refresh
Configuration
React Fast Refresh is enabled via the Vite plugin:vite.config.ts
The
@vitejs/plugin-react-swc uses SWC compiler which provides faster Fast Refresh compared to the Babel-based plugin.What Gets Hot Reloaded
- React Components
- CSS Files
- Modules & Utilities
Component changes apply instantly with state preservation:Behavior:
src/App.tsx
- Edit the JSX or styling → HMR updates, state preserved
- Click button to set count to 5
- Edit the component
- Count remains 5 after HMR update
State Preservation
When State is Preserved
JSX Changes
Modifying component rendering logic preserves state
Style Updates
CSS and className changes preserve state
Event Handlers
Updating onClick and other handlers preserves state
Helper Functions
Non-hook function changes within components preserve state
When State is Reset
Hook Changes
Hook Changes
Adding, removing, or reordering hooks causes state reset:
Component Rename
Component Rename
Renaming a component function causes remount:
Export Changes
Export Changes
Changing default to named export or vice versa:
Development Experience
Instant Feedback
src/App.tsx
- Start dev server:
npm run dev - Click the button several times (e.g., count = 5)
- Edit the text “count is” to “clicks:”
- Save the file
- Notice: count remains 5, only text updates
Fast Build Times
Initial Startup
Vite starts in milliseconds using native ES modules
HMR Updates
Updates apply in less than 100ms regardless of app size
SWC Compilation
20x faster compilation with SWC instead of Babel
No Bundling
Development mode serves unbundled ES modules
Troubleshooting HMR
Common Issues
HMR Not Working
HMR Not Working
Symptoms: Changes require manual page refreshSolutions:
- Check if dev server is running:
npm run dev - Verify WebSocket connection in browser DevTools > Network > WS
- Check for syntax errors in the console
- Restart the dev server
Full Page Reload Instead of HMR
Full Page Reload Instead of HMR
Causes:
- Syntax errors in the file
- Changes to files outside
src/ - Modifications to
vite.config.ts - Changes to
index.html
vite.config.tschanges → requires server restartindex.htmlchanges → requires full reload- Component changes → should use HMR
State Resets Unexpectedly
State Resets Unexpectedly
Common causes:Solution: Always use named function components
Changes Not Reflected
Changes Not Reflected
Checklist:
- ✅ File saved?
- ✅ No TypeScript errors?
- ✅ Component exported correctly?
- ✅ Browser tab is active?
- Press
rin the Vite dev server terminal - Or manually refresh: Ctrl+Shift+R (hard reload)
Performance Tips
HMR API (Advanced)
For custom HMR behavior, use Vite’s HMR API:Most React applications don’t need custom HMR logic. The
@vitejs/plugin-react-swc handles HMR automatically with Fast Refresh.Development Workflow
Dev Server Commands
r- Restart serveru- Show server URLso- Open in browserq- Quit server
Browser DevTools
- Console: Runtime errors and logs
- Network > WS: WebSocket connection
- Elements: Inspect DOM changes
- React DevTools: Component state
