Guide
Using Chakra UI's useBreakpointValue Hook for Responsive Component Styles
Learn how Chakra UI's useBreakpointValue hook returns responsive values based on theme breakpoints, with a working code example, limits, and common mistakes.
Published by Tasadduq Burney
20 Nov 2025, 13:21 UTC
3 min84.4K views0

Quick answer: get a value that changes with the viewport
The useBreakpointValue hook lets you return a different value (like width, font size, or spacing) depending on the current breakpoint defined in your Chakra theme. You write one object that maps breakpoint names to values, and the hook picks the appropriate one as the window size changes.
Worked example
import { useBreakpointValue, ChakraProvider, extendTheme, Box } from '@chakra-ui/react';
// 1. Define breakpoints in the theme (optional if you keep the defaults)
const theme = extendTheme({
breakpoints: ['sm', 'md', 'lg'],
});
function ResponsiveBox() {
// 2. Pass an object where keys match breakpoint names
const width = useBreakpointValue({
base: '100px', // < sm
sm: '200px', // >= sm && < md
md: '300px', // >= md && < lg
lg: '400px', // >= lg
});
// 3. Use the returned value in inline style or a Chakra prop
return ;
}
// App wrapper
function App() {
return (
);
}
export default App;
How the hook works
- When the component mounts,
useBreakpointValuereads the theme’sbreakpointsarray. - It evaluates the supplied object from the smallest breakpoint upward and returns the first value whose breakpoint matches the current viewport width.
- If no breakpoint matches, it falls back to the
basekey (the value with no breakpoint prefix). - The hook subscribes to
window.matchMediainternally, so a change in viewport triggers a re‑render with the new value.
Limits and what the hook does not do
- It only understands breakpoints that exist in the theme; passing a key that isn’t defined will cause the hook to skip it and may return
baseorundefined. - There is no built‑in interpolation between breakpoints; you cannot ask for a value that smoothly transitions from 200px to 300px as the window grows.
- The hook does not replace CSS grid or flexbox layout techniques; it merely supplies a scalar value.
- During server‑side rendering (
windowis undefined) the hook will throw unless you guard it.
Common pitfalls to avoid
- Missing
ChakraProvider: Without the provider the hook cannot read the theme, so it will returnbaseor error. Always wrap your component tree. - Passing a non‑object: The hook expects an object; giving an array or primitive causes a runtime error.
- Expecting automatic updates without React re‑render: The hook relies on React’s state; if you memoize the component incorrectly you may see stale values.
- Using the hook outside a function component: Hooks rules apply; calling it in a class component or a plain function will break.
- SSR mismatch: Rendering the hook on the server leads to a different value than on the client, causing hydration warnings. Guard with
typeof window !== 'undefined'or useuseEffectto set state after mount.
How to verify the behavior
- Render the
ResponsiveBoxin a browser. - Open DevTools, select the element, and watch its
widthstyle change as you resize the window. - You should see the width shift from 100px → 200px → 300px → 400px as you cross the
sm,md,lgbreakpoints defined in the theme. - Optionally, log the value inside a
useEffectto confirm it matches the breakpoint map at each width. - To test the provider dependency, temporarily remove
ChakraProviderand observe that the hook reverts to thebasevalue (or throws if you rely on a missing key).
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.