Skip to main content

useMediaQuery

function useMediaQuery(query: string, defaultValue?: boolean): boolean;

Returns whether the CSS media query matches, and re-renders when it starts or stops matching (for example as the window is resized). Changing query between renders switches to the new query. An invalid query never matches. During server rendering, and on the first render while hydrating, it returns defaultValue (false unless given).

Live demo​

Mobile layout

Resize the window across 768px to switch layouts.

Example​

import { useMediaQuery } from "react-honey-hooks";

function Navigation() {
const isWide = useMediaQuery("(min-width: 768px)");
const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");

return isWide ? <Sidebar animated={!reduceMotion} /> : <BottomTabs />;
}