How it works…

Basically, all you have to do in order to detect any size changes is to produce one or more <MediaQuery> components when rendering, and those whose requirements are met will actually be rendered, and the rest won't appear on the page.

Let's write a very basic example with plenty of media queries, to see the coding style you'll be using. The following is an example given in the react-responsive GitHub page; we'll just try to detect some aspects of the current device and window:

// Source file: /src/App.4.js/* @flow */import React, { Component } from "react";import MediaQuery from "react-responsive";const XS = 576; // phoneconst SM = 768; // tabletconst MD = 992; // desktopconst LG = 1200; // large desktopclass App extends Component<{}> ...

Get Modern JavaScript Web Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.