2.0.0 • Published 3 years ago

react-with-media v2.0.0

Weekly downloads
86
License
(Apache-2.0 OR MI...
Repository
github
Last release
3 years ago

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.

2.0.0

3 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.6.0

5 years ago

0.5.0

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago