Introducing Responsive React Components

26th November 2018 Off By binary
Introducing Responsive React Components
trading bonus no deposit

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 (

I'm a fancy mobile div

I'm a heavy desktop 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.

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(


This component will render only in mobile devices


);
};
const Example = () => {
return(


I will render only in mobile devices




I will render only in tablets (iPad, etc...)




I will render in laptops, desktops and everything bigger



);
};

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 }
]);


Only cats can render me 🐈

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.

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! ⚡️

Originally published at dev.to.


Introducing Responsive React Components 🔥 was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

How to win at binary options?