
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-use-breakpoint
Advanced tools
Use breakpoints in JS when dealing with React based applications
A React hook to get the current breakpoint via the MatchMedia API. Tiny, fast, and easy to use. I created this as I found myself copying this bad boy around every project I did... so I figured I'd make it a package. Other plugins out there are not exactly what I needed, and although I think getting breakpoints in JS should be a last resort. There is undoubtedly a time and place for it. Especially as designers design more creative layouts that are not easily achievable with CSS alone.
Install the package
npm install react-use-breakpoint
yarn add react-use-breakpoint
Wrap your apps root component with the BreakpointProvider
component. This will provide the useBreakpoint
hook to all
child components.
import {BreakpointProvider} from 'react-use-breakpoint';
const App = () => (
<BreakpointProvider>
<MyComponent/>
</BreakpointProvider>
);
The default breakpoint size values are:
Breakpoint | Size (px) |
---|---|
xs | 360 |
sm | 640 |
md | 960 |
lg | 1280 |
xl | 1440 |
You can customise these values xs, sm, md, lg, xl
to fit your needs like so (strings or numbers are accepted):
import {BreakpointProvider} from 'react-use-breakpoint';
const App = () => (
<BreakpointProvider
breakpointOverrides={{
xs: 360,
sm: '400'
}}
>
<MyComponent/>
</BreakpointProvider>
);
Within all child components you can then leverage the useBreakpoint()
hook to get the current breakpoints.
Specifying no arguments will return an object with min and max breakpoints { min: {...}, max: {...} }
. The function
overload handles the types for you 😉.
import {useBreakpoint} from 'react-use-breakpoint';
const MyComponent = () => {
const {min} = useBreakpoint();
const {isXs} = min;
return <div>Current breakpoint: {breakpoint}</div>;
};
If you specify a breakpoint "direction" (min or max) you will get an object back with all the breakpoint sizes and their
assigned boolean
value.
import {useBreakpoint} from 'react-use-breakpoint';
const MyComponent = () => {
const {isXs, isSm, isMd} = useBreakpoint('min');
// isXs = true
// isSm = false
// isMd = false
...
return <div>Current breakpoint: {breakpoint}</div>;
};
Sure... open up an issue, and I'll see what I can do.
FAQs
Use breakpoints in JS when dealing with React based applications
The npm package react-use-breakpoint receives a total of 48 weekly downloads. As such, react-use-breakpoint popularity was classified as not popular.
We found that react-use-breakpoint demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.