one-hook
Size
0.43 kb
View source

useWindowSize

Track the dimensions of the window.

Features

  • Optimized performance: Leverages a shared listener pool to reduce event subscriptions.
  • Custom state tracking: disable internal state tracking with trackState.
  • Event-driven updates: Provides an onChange callback for reacting to dimension changes.

Installation

npm install @1hook/use-window-size

Usage

import {  } from '@1hook/use-window-size'

export const  = ({ : true })

Setting ssr to true initializes width and height as undefined to prevent hydration mismatches between server and client.

const { ,  } = ()

Use trackState: false to disable state tracking, the onChange callback is executed whenever the window size changes.

({
  : false,
  () {
    if (. < 600) {
      // do something
    }
  },
})

API Reference

DefineUseWindowSizeOptions

Options for the `defineUseWindowSize` hook.

PropTypeDefault
ssr?
TSSR
-

UseWindowSizeOptions

Options for the `useWindowSize` hook.

PropTypeDefault
onChange?
((size: { width: number; height: number; }) => void)
-
trackState?
boolean
true

UseWindowSizeReturn

The return value of the `useWindowSize` hook.

PropTypeDefault
height?
number
-
width?
number
-

On this page