🧦 React library to render components only on specific viewports πŸ”₯
Branch: master
Clone or download
Latest commit fdd4cc1 Jan 19, 2019


React Socks

Build Status npm version dependencies Status License: MIT

Wrap your components with React Socks to prevent unnecessary render in different viewports.

<Breakpoint small down>
    This component will render only in mobile devices

Why? start with why

Conventionally we have been writing css media queries for different viewports to hide and show elements that are always present in the DOM. With React taking over the world, everything is about rendering components into the DOM. React Socks helps you conditionally render elements based on viewports.

  1. Render viewport specific components without hassle

  2. You can define your own breakpoints (Eg. xs, ipad, bigmonitors) and use them

  3. You can improve your app performance if you lazy load your viewport specific components

  4. Simpler and sweeter syntax for ease of use


$ npm install --save react-socks


Just wrap your top level component with BreakpointProvider and use the Breakpoint component anywhere you need.

Note: BreakpointProvider was introduced only in v1.0.0. It's not available in previous alpha versions.

import Breakpoint, { BreakpointProvider } from 'react-socks';

// entry file (usually App.js or index.js)
const App = () => (
    <Example />
// Example.js
const Example = () => {
  return (
      <Breakpoint small down>
        <div>I will render only in mobile devices</div>

      <Breakpoint medium only>
        <div>I will render only in tablets (iPad, etc...)</div>

      <Breakpoint medium down>
        <div>I will render in tablets (iPad, etc...) and everything below (mobile devices)</div>

      <Breakpoint medium up>
        <div>I will render in tablets (iPad, etc...) and everything above (laptops, desktops)</div>

      <Breakpoint large up>
        <div>I will render in laptops, desktops and everything above</div>


Set Default Breakpoints

You can define your own breakpoints.

  • Pass an array of objects with the breakpoint name and width in px to setDefaultBreakpoints once in your App.js or your React entry file.

Note: You only need to set default breakpoints once in your app

import { setDefaultBreakpoints } from 'react-socks';

  { xs: 0 },
  { s: 376 },
  { m: 426 },
  { l: 769 },
  { xl: 1025 }

<Breakpoint m only>
    I will render only in m devices
  • You can use any breakpoint name (Eg. cats, puppies, dinosaurs, etc) and width.
  { cats: 0 },
  { dinosaurs: 900 }

<Breakpoint cats only>
    Only cats can render me
  • If you don't set a default breakpoint, the library will fallback to Bootstrap 4 default breakpoints as described below.
  { xsmall: 0 }, // all mobile devices
  { small: 576 }, // mobile devices (not sure which one's this big)
  { medium: 768 }, // ipad, ipad pro, ipad mini, etc
  { large: 992 }, // smaller laptops
  { xlarge: 1200 } // laptops and desktops


Import the Breakpoint component anywhere in the your code and start using it with your breakpoint and modifier props.

// small is breakpoint
// down is modifier
<Breakpoint small down>
    This component will render only in mobile devices

You have three modifiers

  • only - will render the component only in the specified breakpoint.

  • up - will render the component in the specified breakpoint and all the breakpoints above it (greater than the width).

  • down - will render the component in the specified breakpoint and all the breakpoints below it (less than the width).


Thanks goes to these amazing people πŸŽ‰

Dinesh Pandiyan






MIT Β© Dinesh Pandiyan