# prefer-observer-apis

Rule `unicorn/prefer-observer-apis`. Prefer observer APIs over resize and scroll listeners with layout reads.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #1074 by reach, #1329 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-observer-apis` | no | no | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-observer-apis.md) |

## Code examples

### Incorrect: the rule reports this

```js
window.addEventListener('resize', () => {
  element.classList.toggle('is-small', element.offsetWidth < 500);
});
```

```js
window.addEventListener('scroll', () => {
  element.classList.toggle('is-visible', element.getBoundingClientRect().top < window.innerHeight);
});
```

### Correct: the rule accepts this

```js
new ResizeObserver(entries => {
  for (const entry of entries) {
    entry.target.classList.toggle('is-small', entry.contentRect.width < 500);
  }
}).observe(element);
```

```js
new IntersectionObserver(entries => {
  for (const entry of entries) {
    entry.target.classList.toggle('is-visible', entry.isIntersecting);
  }
}).observe(element);
```

```js
window.addEventListener('scroll', () => {
  updateScrollPosition(window.scrollY);
});
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-observer-apis": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-object-destructuring-defaults](https://lintradar.dev/rules/unicorn/prefer-object-destructuring-defaults.md): Prefer object destructuring defaults over default object literals with spread.
- [unicorn/prefer-options-parameter](https://lintradar.dev/rules/unicorn/prefer-options-parameter.md): Prefer an options object over optional positional parameters.
- [unicorn/prefer-number-is-safe-integer](https://lintradar.dev/rules/unicorn/prefer-number-is-safe-integer.md): Prefer `Number.isSafeInteger()` over integer checks.
- [unicorn/prefer-private-class-fields](https://lintradar.dev/rules/unicorn/prefer-private-class-fields.md): Prefer private class fields over the underscore-prefix convention.
- [unicorn/prefer-location-assign](https://lintradar.dev/rules/unicorn/prefer-location-assign.md): Prefer `location.assign()` over assigning to `location.href`.
- [unicorn/prefer-promise-try](https://lintradar.dev/rules/unicorn/prefer-promise-try.md): Prefer `Promise.try()` over promise-wrapping boilerplate.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-observer-apis. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
