Giter Site home page Giter Site logo

react-use-media's Introduction

react-use-media

React hook for testing and monitoring media queries.

Travis Codecov

Installation

$ npm install --save react-use-media

Additionally, you'll need to install version 16.7.0-alpha.0 of react and react-dom since this package relies on React Hooks:

DISCLAIMER: React Hooks are an experimental proposal. The Hooks API, as well as this library's, are unstable and subject to change.

Usage

import React from 'react';
import useMedia from 'react-use-media';

function MyComponent() {
  const matches = useMedia('(max-width: 599px)');
  return matches ? (
    <p>The document is less than 600px wide.</p>
  ) : (
    <p>The document is at least 600px wide.</p>
  );
}

API Reference

useMedia

const matches = useMedia(mediaQueryString, options);

Accepts a media query string as the first argument (mediaQueryString) and returns whether it matches or not.

Optionally, you can pass a second argument to useMedia that is an object with the following properties:

  • initialMatches (boolean): The value of matches on first mount. Defaults to true.

react-use-media's People

Contributors

olistic avatar

Watchers

James Cloos avatar Jonny Burger avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.