
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
@buildinams/use-match-media
Advanced tools
Stateful hook that uses the matchMedia API.
This hook optimizes the use of the match media API by only creating a new listener when a unique query is made, avoiding the creation of unnecessary listeners and increasing efficiency.
This library is also SSR safe, and a default value can be provided for the initial render.
Install this package with npm
.
npm i @buildinams/use-match-media
To use the library simply import the hook and pass any custom media query.
import useMatchMedia from "@buildinams/use-match-media";
const MyComponent = () => {
const isTouch = useMatchMedia("(pointer: coarse)");
...
};
You can even use multiple queries in a single call. Though we recommend using separate hooks for each query to maximize performance.
import useMatchMedia from "@buildinams/use-match-media";
const MyComponent = () => {
const isTouchAndPortrait = useMatchMedia("(pointer: coarse) and (orientation: portrait)");
...
};
If you want to provide a default value for the initial render (and in server), you can pass it as defaultValue
within the optional config object. This accepts boolean
, undefined
, or null
. For example:
import useMatchMedia from "@buildinams/use-match-media";
const MyComponent = () => {
const isSmall = useMatchMedia("(max-width: 768px)", { defaultValue: true });
...
};
Couple things to note:
false
.You can conditionally listen to events by passing a isEnabled
prop in the config object. This accepts a boolean
value, and will only listen to events if the value is true
(default). For example:
import useMatchMedia from "@buildinams/use-match-media";
const MyComponent = () => {
const [isEnabled, setIsEnabled] = useState(false);
const isSmall = useMatchMedia("(max-width: 768px)", { isEnabled });
...
};
By default, the hook will use useEffect
to listen to events. However, you can use useLayoutEffect
instead by passing layoutEffect
in the config object. For example:
import useMatchMedia from "@buildinams/use-match-media";
const MyComponent = () => {
const isSmall = useMatchMedia("(max-width: 768px)", { layoutEffect: true });
...
};
This is SSR safe, and will only use useLayoutEffect
on the client.
This library requires a minimum React version of 17.0.0
.
Found an issue? Want a new feature? Get involved! Please contribute using our guideline here.
FAQs
Stateful hook that uses the matchMedia API.
We found that @buildinams/use-match-media demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.