Skip to content

@technobuddha > react-hooks > React > Hooks

Function: useHMR()

ts
function useHMR(): number;

Defined in: use-hmr.ts:32

React hook that tracks Hot Module Replacement (HMR) updates in a Vite development environment.

DANGER

This hook relies on Vite's HMR API and will not function outside of a Vite development environment.

Each time a HMR update occurs, the returned number increments, allowing components to respond to HMR events.

Returns

number

The current HMR update count.

Example

typescript
function MyComponent() {
  const hmrCount = useHMR();
  return <div>HMR updates: {hmrCount}</div>;
}

Remarks

In a non-Vite environment or in a production environment, this hook will always return 0, and never update.