This hook takes a media query string and returns an array with a boolean (matches
) and a way to set the media query (setMediaQuery
)
Example: https://codepen.io/tbremer/pen/QWWEwXr?editors=1010
To use:
function Application() {
const [matches] = useMatchMedia('(max-width: 600px)');
return (
<div style={{ color: matches ? 'blue' : 'red' }}>
<h1>Welcome to React v{React.version}!</h1>
</div>
);
}
ReactDOM.render(
<Application />,
document.getElementById('app')
);
useMatchMedia(query: string)
: returns an array with two items
[
matches: boolean,
setMediaQuery: (string) => void // function that takes a query (string) as it's argument.
]