npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@react-hook/window-size

v3.1.1

Published

React hooks for updating components when the size of the `window` changes.

Downloads

644,419

Readme

React hooks for updating components when the size or orientation of the window changes. These hooks come in two forms: debounced using useDebounce() and throttled using useThrottle().

Quick Start

Check out the example on CodeSandbox

//
// Debounced values
import {
  useWindowSize,
  useWindowWidth,
  useWindowHeight,
} from '@react-hook/window-size'

const Component = (props) => {
  const [width, height] = useWindowSize()
  const onlyWidth = useWindowWidth()
  const onlyHeight = useWindowHeight()
}

//
// Throttled values
import {
  useWindowSize,
  useWindowWidth,
  useWindowHeight,
} from '@react-hook/window-size/throttled'

const Component = (props) => {
  const [width, height] = useWindowSize()
  const onlyWidth = useWindowWidth()
  const onlyHeight = useWindowHeight()
}

API

useWindowSize(options?): [number, number]

A hook that returns the current width and height of the window. This hook is debounced, meaning it will wait (100ms by default) for the resize events to stop firing before it actually updates its state with the new width and height.

Options

| | Type | Required? | Description | | ------- | --------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------ | | options | DebouncedWindowSizeOptions | No | Options for configuring the hook. See DebouncedWindowSizeOptions below. |

DebouncedWindowSizeOptions

| Key | Type | Default | Description | | ------------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | wait | number | 100 | The amount of time in ms you want to wait after the latest resize event before updating the size of the window in state. | | leading | boolean | false | When true, updates the size of the window on the leading edge (right away) in addition to debouncing any additional events. | | initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR | | initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |

Returns [width: number, height: number]

| | Type | Description | | ------ | -------- | ---------------------------------------- | | width | number | The current clientWidth of the window | | height | number | The current clientHeight of the window |


useWindowWidth(options?): number

A hook that returns the current width of the window. This hook is debounced, meaning it will wait (100ms by default) for the resize events to stop firing before it actually updates its state with the new width.

Options

| | Type | Required? | Description | | ------- | ----------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------- | | options | DebouncedWindowSizeOptions | No | Options for configuring the hook. See DebouncedWindowSizeOptions below. |

DebouncedWindowSizeOptions

| Key | Type | Default | Description | | ------------ | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | wait | number | 100 | The amount of time in ms you want to wait after the latest resize event before updating the size of the window in state. | | leading | boolean | false | When true, updates the size of the window on the leading edge (right away) in addition to debouncing any additional events. | | initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR |

Returns width: number

| | Type | Description | | ----- | -------- | --------------------------------------- | | width | number | The current clientWidth of the window |


useWindowHeight(options?): number

A hook that returns the current height of the window. This hook is debounced, meaning it will wait (100ms by default) for the resize events to stop firing before it actually updates its state with the new height.

Options

| | Type | Required? | Description | | ------- | ----------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------- | | options | DebouncedWindowSizeOptions | No | Options for configuring the hook. See DebouncedWindowSizeOptions below. |

DebouncedWindowSizeOptions

| Key | Type | Default | Description | | ------------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | wait | number | 100 | The amount of time in ms you want to wait after the latest resize event before updating the size of the window in state. | | leading | boolean | false | When true, updates the size of the window on the leading edge (right away) in addition to debouncing any additional events. | | initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |

Returns height: number

| | Type | Description | | ------ | -------- | ---------------------------------------- | | height | number | The current clientHeight of the window |


Throttled API

To use these throttled hooks instead of debounced hooks, import with import {...} from '@react-hook/window-size/throttled

useWindowSize(options?): [number, number]

A hook that returns the current width and height of the window. This hook is throttled, meaning it will only update its state at most 30fps (by default, configuration below) with the new width and height of the window. It will always update at the trailing edge, so you don't have to worry about not having the correct width or height after the window is finished resizing. It will also update at the leading edge if configured to do so.

Options

| | Type | Required? | Description | | ------- | --------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------ | | options | ThrottledWindowSizeOptions | No | Options for configuring the hook. See ThrottledWindowSizeOptions below. |

ThrottledWindowSizeOptions

| Key | Type | Default | Description | | ------------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | fps | number | 30 | The rate in frames per second at which the size of the window is updated | | leading | boolean | false | When true, updates the size of the window on the leading edge (right away) in addition to throttling any additional events. | | initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR | | initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |

Returns [width: number, height: number]

| | Type | Description | | ------ | -------- | ---------------------------------------- | | width | number | The current clientWidth of the window | | height | number | The current clientHeight of the window |


useWindowWidth(options?): number

A hook that returns the current width of the window. This hook is throttled, meaning it will only update its state at most 30fps (by default, configuration below) with the new width of the window. It will always update at the trailing edge, so you don't have to worry about not having the correct width after the window is finished resizing. It will also update at the leading edge if configured to do so.

Options

| | Type | Required? | Description | | ------- | ----------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------- | | options | ThrottledWindowSizeOptions | No | Options for configuring the hook. See ThrottledWindowSizeOptions below. |

ThrottledWindowSizeOptions

| Key | Type | Default | Description | | ------------ | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | fps | number | 30 | The rate in frames per second at which the size of the window is updated | | leading | boolean | false | When true, updates the size of the window on the leading edge (right away) in addition to throttling any additional events. | | initialWidth | number | 0 | The initial width to use when there is no window object, e.g. SSR |

Returns width: number

| | Type | Description | | ----- | -------- | --------------------------------------- | | width | number | The current clientWidth of the window |


useWindowHeight(options?): number

A hook that returns the current height of the window. This hook is throttled, meaning it will only update its state at most 30fps (by default, configuration below) with the new height of the window. It will always update at the trailing edge, so you don't have to worry about not having the correct height after the window is finished resizing. It will also update at the leading edge if configured to do so.

Options

| | Type | Required? | Description | | ------- | ----------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------- | | options | ThrottledWindowSizeOptions | No | Options for configuring the hook. See ThrottledWindowSizeOptions below. |

ThrottledWindowSizeOptions

| Key | Type | Default | Description | | ------------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | | fps | number | 30 | The rate in frames per second at which the size of the window is updated | | leading | boolean | false | When true, updates the size of the window on the leading edge (right away) in addition to throttling any additional events. | | initialHeight | number | 0 | The initial height to use when there is no window object, e.g. SSR |

Returns height: number

| | Type | Description | | ------ | -------- | ---------------------------------------- | | height | number | The current clientHeight of the window |

LICENSE

MIT