useBreakpointValue
useBreakpointValue
is a custom hook which returns the value for the current
breakpoint from the provided responsive values object. This hook also responds
to the window resizing and returning the appropriate value for the new window
size.
Tip
Using JavaScript for responsive design should be a last resort. Chances are what you're trying to do can be accomplished with pure CSS via responsive style props.
Import
import { useBreakpointValue } from '@hoverinc/design-system-react-web';
Return value
The useBreakpointValue
hook returns the value for the current breakpoint.
Usage
Make sure to provide a fallback
value when using useBreakpointValue
with SSR
so it doesn't return undefined
in the first render.
useBreakpoint
The useBreakpoint
hook simply returns the current responsive breakpoint token.
Server-Side Rendering (SSR)
These hooks are built to work in server-side rendering (SSR) applications by default. You might notice a quick flash of incorrect media query values when you use them.
If you're creating a client-side rendered app, you can leverage the ssr
argument to get the correct value on the first render.
const buttonSize = useBreakpointValue( { base: 'small', tablet: 'medium' }, { ssr: false },);const breakpoint = useBreakpoint({ ssr: false });