React Media Query Example

In this example below you will see how to do a React Media Query Example with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by naturalclar, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright naturalclar ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>React Media Query Example</title>
  
  
  
  
  
</head>

<body>

  <div id='root'></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/naturalclar/react-media-query-example-LzGdEm */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var MediaQuery = function (_React$Component) {
  _inherits(MediaQuery, _React$Component);

  function MediaQuery(props) {
    _classCallCheck(this, MediaQuery);

    var _this = _possibleConstructorReturn(this, (MediaQuery.__proto__ || Object.getPrototypeOf(MediaQuery)).call(this, props));

    _this.state = {
      query: _this.props.query ? _this.props.query : '(min-width: 760px)',
      queryMatches: false
    };
    return _this;
  }

  /* Control Media Query */


  _createClass(MediaQuery, [{
    key: 'updateMatches',
    value: function updateMatches() {
      this.setState({ queryMatches: this.mediaQuery.matches });
    }
  }, {
    key: 'componentWillMount',
    value: function componentWillMount() {
      this.mediaQuery = window.matchMedia(this.state.query);
      this.mediaQuery.addListener(this.updateMatches.bind(this));
      this.updateMatches();
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.mediaQuery.removeListener(this.updateMatches);
    }
  }, {
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        null,
        this.state.queryMatches ? this.props.children : null
      );
    }
  }]);

  return MediaQuery;
}(React.Component);

var Example = function (_React$Component2) {
  _inherits(Example, _React$Component2);

  function Example() {
    _classCallCheck(this, Example);

    return _possibleConstructorReturn(this, (Example.__proto__ || Object.getPrototypeOf(Example)).apply(this, arguments));
  }

  _createClass(Example, [{
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        null,
        React.createElement(
          'h1',
          null,
          'React Media Query Example'
        ),
        React.createElement(
          'p',
          null,
          'This is a custom mock media-query component that will only display contents when it satisfies the set \'query\' property.'
        ),
        React.createElement(
          'p',
          null,
          'The text below will change according to the window width.'
        ),
        React.createElement(
          MediaQuery,
          { query: '(max-width: 759px)' },
          'Current Viewport is ',
          React.createElement(
            'strong',
            null,
            'Small'
          )
        ),
        React.createElement(
          MediaQuery,
          { query: '(min-width: 760px) and (max-width:999px)' },
          'Current Viewport is ',
          React.createElement(
            'strong',
            null,
            'Medium'
          )
        ),
        React.createElement(
          MediaQuery,
          { query: '(min-width: 1000px) and (max-width:1399px)' },
          'Current Viewport is ',
          React.createElement(
            'strong',
            null,
            'Large'
          )
        ),
        React.createElement(
          MediaQuery,
          { query: '(min-width: 1400px)' },
          'Current Viewport is ',
          React.createElement(
            'strong',
            null,
            'Extra-Large'
          )
        )
      );
    }
  }]);

  return Example;
}(React.Component);

/* main.js */


var Main = function (_React$Component3) {
  _inherits(Main, _React$Component3);

  function Main() {
    _classCallCheck(this, Main);

    return _possibleConstructorReturn(this, (Main.__proto__ || Object.getPrototypeOf(Main)).apply(this, arguments));
  }

  _createClass(Main, [{
    key: 'render',
    value: function render() {
      return React.createElement(
        'div',
        null,
        React.createElement(Example, null)
      );
    }
  }]);

  return Main;
}(React.Component);

;

ReactDOM.render(React.createElement(Main, null), document.querySelector('#root'));

Comments