react-with-media
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

react-with-media

Watch media queries in React components. A hook, A HOC and a render prop component are provided so that you can use whatever flavor you prefer.

Typescript typings are included.

Install

yarn add react-with-media
npm install --save react-with-media

Usage

Hook

const ShowMessage = () => {
  const matches = useMedia('(max-width: 500px)')

  return matches ? <span>Is mobile</span> : <span>Is desktop</span>
}

HOC

const ShowMessage = withMedia('(max-width: 500px)')(({matches}) =>
  matches ? <span>Is mobile</span> : <span>Is desktop</span>,
)

Optionally, the property name can be changed:

const ShowMessage = withMedia('(max-width: 500px)', {name: 'isMobile'})(
  ({isMobile}) => (isMobile ? <span>Is mobile</span> : <span>Is desktop</span>),
)

Render props

const ShowMessage = () => (
  <WithMedia query="(max-width: 500px)">
    {matches => (matches ? <span>Is mobile</span> : <span>Is desktop</span>)}
  </WithMedia>
)

License

react-with-media is dual-licensed under Apache 2.0 and MIT terms.

Readme

Keywords

none

Package Sidebar

Install

npm i react-with-media

Weekly Downloads

35

Version

2.0.0

License

(Apache-2.0 OR MIT)

Unpacked Size

38.5 kB

Total Files

21

Last publish

Collaborators

  • pajn