# use-device-pixel-ratio

> React hook for finding device pixel ratio (DPR), optionally capping/rounding

Latest version **1.1.2** (published 2022-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-device-pixel-ratio
pnpm add use-device-pixel-ratio
yarn add use-device-pixel-ratio
bun add use-device-pixel-ratio
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-12-13 |
| First published | 2021-08-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 32.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Espen Hovlandsdal |
| Maintainers | rexxars |
| Keywords | react, hook, dpr, dpi, pixel |

## Links

- npm: https://www.npmjs.com/package/use-device-pixel-ratio
- Repository: https://github.com/rexxars/use-device-pixel-ratio
- Homepage: https://github.com/rexxars/use-device-pixel-ratio#readme
- Issues: https://github.com/rexxars/use-device-pixel-ratio/issues
- npm.io page: https://npm.io/package/use-device-pixel-ratio

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2022-12-13
- 1.1.1 — 2022-12-13
- 1.1.0 — 2021-09-01
- 1.0.1 — 2021-08-31
- 1.0.0 — 2021-08-31

## README

# use-device-pixel-ratio

[![github status checks](https://badgen.net/github/checks/rexxars/use-device-pixel-ratio)](https://github.com/rexxars/use-device-pixel-ratio/actions) [![bundlephobia](https://badgen.net/bundlephobia/minzip/use-device-pixel-ratio)](https://bundlephobia.com/result?p=use-device-pixel-ratio)

`useDevicePixelRatio()` is a React hook (and utility) that will tell you what the current device has as its Device Pixel Ratio (DPR). The hook is reactive - if the browser window moves to a different display with a different DPR, it will update automatically. If you only need to get the DPR statically, there is a function (`getDevicePixelRatio()`) equivalent.

## Installing

```
npm i use-device-pixel-ratio
```

## Server rendering

When rendering on the server or in browsers that do not support the `devicePixelRatio` property, it will default to `1` unless overriden by using the `defaultDpr` option.

## Rounding/limiting

The hook (by default) both rounds and limits the DPR - it will round _down_ by default, and cap the number at `3`. In other words, you should by default only have three values returned: `1`, `2` or `3`. To allow larger DPRs, pass a higher number to the `maxDpr` option. To prevent rounding, pass `round: false`.

## Usage

**Default usage**

```jsx
import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio()
  return <img src={`https://my.image.host/file.jpg?dpr=${dpr}`}>
}
```

**Setting higher limit**

```jsx
import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio({maxDpr: 50})
  return <div>DPR is {dpr}</div>
}
```

**Getting the "raw" DPR**

```jsx
import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio({maxDpr: +Infinity, round: false})
  return <div>DPR is {dpr}</div>
}
```

**Setting the default DPR**

```jsx
import {useDevicePixelRatio} from 'use-device-pixel-ratio'

async function MyComponent() {
  const dpr = useDevicePixelRatio({defaultDpr: 2})

  // Actual device DPR if available, 2 otherwise
  return <div>DPR is {dpr}</div>
}
```

## Function usage

```ts
import {getDevicePixelRatio} from 'use-device-pixel-ratio'

console.log('Device pixel ratio is ', getDevicePixelRatio())

// Or, with options (same options as hook):
console.log('Device pixel ratio is ', getDevicePixelRatio({maxDpr: 5}))
```

## License

MIT © [Espen Hovlandsdal](https://espen.codes/)

---
_Source: https://npm.io/package/use-device-pixel-ratio · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
