
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
react-media-hook2
Advanced tools
[](https://npmjs.org/package/react-media-hook2) [](https://npmjs.org/package/react-media-hook2) [![
English | 简体中文
$ npm install react-media-hook2 --save
or
$ yarn add react-media-hook2
interface UseMediaProps {
defaultMatches?: boolean;
id?: any;
onChange?: (matches: boolean) => void | boolean;
paused?: boolean;
query?: string | MediaQueryProperties | MediaQueryProperties[];
targetWindow?: Window;
}
import useMedia from 'react-media-hook2';
export default () => {
const [matches, setProps] = useMedia({ query: '(max-width: 600px)' });
return <div>Width of window is {matches ? 'less' : 'greater'} than 600px.</div>;
};
MediaQueryPropertiesimport useMedia from 'react-media-hook2';
export default () => {
const [matches, setProps] = useMedia({ query: { maxWidth: 600 } });
return <div>Width of window is {matches ? 'less' : 'greater'} than 600px.</div>;
};
For example, when the screen width changes, let the side menu expand or collapse once automatically.
import { useState } from 'react';
import useMedia from 'react-media-hook2';
export default () => {
const [collapsed, setCollapsed] = useState(false);
const [matches, setProps] = useMedia({ query: { maxWidth: 600 }, onChange: setCollapsed });
return <MenuComponen collapsed={collapsed} onCollapsed={setCollapsed} />;
};
Tips: if onChange return true, useMedia will not change the matches this time.
getUseMediaSometimes we need to use the same media query in many components to achieve responsiveness, so getUseMedia is provided for you to get the hook created in other components.
import ChildComponent from './example';
import useMedia from 'react-media-hook2';
export default () => {
const [matches, setProps] = useMedia({ id: 0, query: { maxWidth: 600 } });
return (
<div>
<div>Width of window is {matches ? 'less' : 'greater'} than 600px.</div>
<ChildComponent />
</div>
);
};
// `./example`
import { getUseMedia } from 'react-media-hook2';
export default () => {
const [matches, setProps] = getUseMedia(0);
return <div>matches: {matches}</div>
}
You can pause listener to provide additional desktop version on mobile devices.
import { useState } from 'react';
import useMedia from 'react-media-hook2';
export default () => {
const [matches, setProps] = useMedia({ query: '(max-width: 600px)' });
return (
<div>
<div>Width of window is {matches ? 'less' : 'greater'} than 600px.</div>
<button onClick={() => setProps(prevProps => ({ ...prevProps, paused: true }))}>
Pause listener
</button>
</div>
);
};
import { useState } from 'react';
import useMedia from 'react-media-hook2';
export default () => {
const [matches, setProps] = useMedia({ query: '(max-width: 600px)' });
const setRandomValue = () =>
setProps(prevProps => ({ ...prevProps, query: { maxWidth: Math.Random() * 1000 } }));
return (
<div>
<div>Width of window is {matches ? 'less' : 'greater'} than 600px.</div>
<button onClick={setRandomValue}>Set a random value</button>
</div>
);
};
You can use enum to ensure that the id is globally unique:
import React from 'react';
import useMedia from 'react-media-hook2';
export enum GlobalId {
MyComponent,
}
export default () => {
const [matches, setProps] = useMedia({ id: GlobalId.MyComponent, query: '(max-width: 600px)' });
return <div>Width of window is {matches ? 'less' : 'greater'} than 600px</div>;
};
FAQs
[](https://npmjs.org/package/react-media-hook2) [](https://npmjs.org/package/react-media-hook2) [
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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.