useMediaQuery

Subscribe and respond to media query changes with useMediaQuery.

Install:

npm i @uidotdev/usehooks

Description:

The useMediaQuery hook leverages the window.matchMedia API to subscribe to CSS media query changes, thereby providing real-time responsiveness to dynamic changes in the viewport or screen orientation. It allows the component to rerender whenever the media query’s result changes. It throws an error if attempted to be used on the server-side (media queries only work in the browser).

Parameters

NameTypeDescription
querystringThe media query to listen changes

Return Value

TypeDescription
booleanReturns a boolean value indicating whether the media query matches the current state of the device.

Demo:

Example:

More Hooks:

Sort: