0.9.0 • Published 2 years ago

@better-typed/react-window-hooks v0.9.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Window Hooks

NPM npm bundle size npm type definitions NPM npm GitHub stars

Handle window events and observe window size

Features

  • :rocket: Simple, fast and light
  • :factory: Observe window size
  • 🪗 Lifecycle window events handling

Install

npm install --save @better-typed/react-window-hooks

or

yarn add @better-typed/react-window-hooks

useWindowEvent

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 (
    // ...
  )
}
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 (
    // ...
  )
}

useWindowSize

import React from "react";
import { useWindowSize } from "@better-typed/react-window-hooks";

const MyComponent: React.FC = () => {
  // Updates with resizing
  const [width, height] = useWindowSize()

  return (
    // ...
  )
}