@better-typed/react-window-hooks
Version:
React window hooks
84 lines (60 loc) • 1.79 kB
Markdown
[](https://www.npmjs.com/package/@better-typed/react-window-hooks)





> Handle window events and observe window size
- [Better Typed](https://github.com/BetterTyped)
- :rocket: Simple, fast and light
- :factory: Observe window size
- 🪗 Lifecycle window events handling
```bash
npm install --save @better-typed/react-window-hooks
```
or
```bash
yarn add @better-typed/react-window-hooks
```
```tsx
import React from "react";
import { useWindowEvent } from "@better-typed/react-window-hooks";
const MyComponent: React.FC = () => {
// Unmounts event with component lifecycle
useWindowEvent("resize", () => {
// ... Do something
});
return (
// ...
)
}
```
```tsx
import React from "react";
import { useWindowEvent } from "@better-typed/react-window-hooks";
const MyComponent: React.FC = () => {
// Unmounts event with component lifecycle
useWindowEvent("scroll", () => {
// ... Do something
});
return (
// ...
)
}
```
```tsx
import React from "react";
import { useWindowSize } from "@better-typed/react-window-hooks";
const MyComponent: React.FC = () => {
// Updates with resizing
const [width, height] = useWindowSize()
return (
// ...
)
}
```