useMedia

useMedia is an custom hook for utilizing media queries in your component logic.

Import#

import { useMedia } from '@dwarvesf/react-hooks'

Usage#

function Example() {
const columnCount = useMedia(
['(min-width: 1500px)', '(min-width: 1000px)', '(min-width: 600px)'],
[5, 4, 3],
2,
)
return <div>Resize your browser window: {columnCount}</div>
}
function Example() {
const isMobile = useMedia(['(max-width: 512px)'], [true], false)
return <div>{isMobile ? 'Mobile view' : 'Desktop view'}</div>
}

Parameters#

The useDisclosure hook accepts an list of following paramesters:

useMedia(queries, values, defaultValue)
NameTypeDefaultDescription
queriesstring[]_The list of media queries.
valuesany[]_The values relate to the list of media queries by array index.
defaultValueany_The default value if none of the values matches the media queries.
Edit this page

Made with ❤️ by Dwarves Foundation