Responsive React Components

November 20, 2018

This article is also cross-posted in —

DEV - Introducing Responsive React Components

HackerNoon - Introducing Responsive React Components

codeburst - Introducing Responsive React Components


Responsive React Components

TL;DR - You can render viewport specific components in React with a one-liner

React is awesome and the whole world agrees to it unanimously. Development is a whole lot faster and easier when we look at everything as components. Since everything is JavaScript driven, React renders only the code that is necessary based on application's state. It doesn't matter if you have over a thousand components and tens of thousands of lines of code. If you lazy load your components, you load only what's necessary for the user and I think that is the biggest win with using React.

That being said, have you ever wondered what happens when you write media queries in your React codebase?

You render elements that's not supposed to be in a particular viewport and hide it using css.

In this example

import React from 'react'
import './Example.style.scss'

const Example = () => {
  return (
    <div className="example">
      <div className="mobile-only">I'm a fancy mobile div</div>
      <div className="desktop-only">I'm a heavy desktop div</div>
    </div>
  )
}

Example.style.scss file

.example {
  .mobile-only {
    @media (min-width: 768px) {
      display: none;
    }
  }
  .desktop-only {
    @media (max-width: 767px) {
      display: none;
    }
  }
}

When Example component renders, both .mobile-only and .desktop-only elements will be rendered in the DOM but .mobile-only div will be hidden in bigger viewports and .desktop-only div will be hidden in smaller viewports with css display: none.

This is okay if this is small. But in one of the projects I worked, we had a heavy desktop menu and a heavy mobile menu both rendered in all the viewports. Just the Menu alone should be around 20Kb in size each and we easily had an unwanted 20Kb being loaded into the browser for each user. If you have more viewport specific elements, this size is going to keep increasing.

Introducing React Socks

React Socks is a minimal React library to render components based on viewport.

Say goodbye to media-queries. Here's how you can render viewport specific components with an uber-cool syntax.

const Example = () => {
  return (
    <Breakpoint small down>
      <MyAwesomeMobileMenu>
        This component will render only in mobile devices
      </MyAwesomeMobileMenu>
    </Breakpoint>
  )
}
const Example = () => {
  return (
    <div>
      <Breakpoint small down>
        <div>I will render only in mobile devices</div>
      </Breakpoint>

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

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

And that's not just it. You can specify your own breakpoints (as many as you want wow!) and use them according to your project needs. Also, you will have to setDefaultBreakpoints only once in your project 😎

import { setDefaultBreakpoints } from 'react-socks'

setDefaultBreakpoints([
  { xs: 0 },
  { s: 376 },
  { m: 426 },
  { l: 769 },
  { xl: 1025 },
])

These are my favourite breakpoints

setDefaultBreakpoints([{ cats: 0 }, { dinosaurs: 900 }])

<Breakpoint cats only>
  Only cats can render me 🐈
</Breakpoint>

Reasons why you should use React Socks

  • Render viewport specific components without hassle
  • You can define your own breakpoints (Eg. xs, ipad, bigmonitors) and use them
  • You can improve your app performance if you lazy load your viewport specific components
  • Simpler and sweeter syntax for ease of use

The library has been published to npm and is in alpha version. I'd love to get your feedback and improve it before releasing the first stable version.

Edit: The first stable version has been released on Dec 9, 2018 with performance improvements and SSR support. 🎉

If you're wondering why the name React Socks 🤷‍♂️

React Socks wraps your components comfortably to prevent unnecessary render in various viewports, just like how you wrap your feet with socks to prevent cold feet 🎉

Let's put some fancy React Socks on and wrap all the components 🔥

You are amazing! Have a great day! ⚡️