Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@better-hooks/window

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@better-hooks/window

React window hooks

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

⚙️ React Window hooks

About

Handle window events and observe window size

Key Features

🔮 Simple usage

🚀 Fast and light

Lifecycle Window events

💎 Lifecycle Document events

🎯 Window size

🪄 Window scroll position

💡 Window focus

🎊 SSR Support

Installation

npm install --save @better-hooks/window

or

yarn add @better-hooks/window

Examples

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

const MyComponent: React.FC = () => {
  // Unmounts event with component lifecycle
  useWindowEvent("scroll", () => {
    // ... Do something
  });

  useWindowEvent("wheel", () => {
    // ... Do something
  });

  useWindowEvent("resize", () => {
    // ... Do something
  });

  return (
    // ...
  )
}


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

const MyComponent: React.FC = () => {
  // Unmounts event with component lifecycle
  useDocumentEvent("visibilitychange", () => {
    // ... Do something
  });

  useDocumentEvent("scroll", () => {
    // ... Do something
  });

  return (
    // ...
  )
}


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

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

  return (
    // ...
  )
}


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

const MyComponent: React.FC = () => {
  // Updates when scrolling
  const [x, y] = useWindowScroll()

  return (
    // ...
  )
}


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

const MyComponent: React.FC = () => {
  // Updates when user leave our page
  const focus = useWindowFocus()

  useEffect(() => {
    if(focus) {
      // User is using our page
    } else {
      // User has minimized window or leaved our page to different tab
    }
  }, [focus])

  return (
    // ...
  )
}

Keywords

FAQs

Package last updated on 29 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc