useDevicePixelRatio
useDevicePixelRatio is a hook that returns the device pixel ratio of the screen.
useDevicePixelRatio reactively tracks window.devicePixelRatio, which indicates the ratio of physical pixels to CSS pixels on the current display. It returns an object with a pixelRatio property that updates automatically when the user moves the window between displays with different densities or changes their system zoom level.
When to Use
- Rendering high-DPI (Retina) images or canvas elements at the correct resolution
- Adjusting visual fidelity or detail level based on display density
- Debugging or displaying device display characteristics in developer tools
Notes
- SSR-safe: Returns a default
pixelRatioof1during server-side rendering. Nowindowaccess occurs on the server. - Reactive updates: Listens for changes via
matchMediaso the value updates when display density changes (e.g., dragging a window between monitors). - Related hooks: See also
useMediaQueryfor general media query tracking.
Usage
Live Editor
function Demo() { const { pixelRatio } = useDevicePixelRatio(); return <p>Device pixel ratio: {pixelRatio}</p>; };
Result
API
UseDevicePixelRatio
Returns
UseDevicePixelRatioReturn
Arguments
UseDevicePixelRatioReturn
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| pixelRatio | Pixel ratio | number (Required) | - |