Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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.
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.
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.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.