I am looking for some recommendations on what libraries, components, approaches to use when building a fully RESPONSIVE React app today?
Should I base on hooks or old school media queries? Maybe some better solution somewhere out there ?
I am looking for some recommendations on what libraries, components, approaches to use when building a fully RESPONSIVE React app today?
Should I base on hooks or old school media queries? Maybe some better solution somewhere out there ?
I suggest using a component library, for the latest components, a responsive component library such as rsuitejs or material design components should do just fine.
Links:
Have a look at Antd or material-ui
or you can use regular CSS & style your components (my favourite way)
Like others mentioned, there are quite a few packages out there for responsive design- CSS frameworks, React component libraries, etc. It is also not hard to roll your own using flexbox and/or grid.
As far as React-centric approaches go, here is one that is pretty efficient. It assumes you are utilizing SSR.
Use Context API to create a Breakpoint Provider component. Its job is to keep track of the current breakpoint, and listen to the resize event to change state as needed. In the constructor, accept the default viewport size. This can be populated server-side by utilizing user-agent sniffing.
Use the Consumer from your Breakpoint Context to expose the current viewport size. This can be used to conditionally load components based on viewport, instead of rendering them and then hiding them with CSS with media queries. You are sending less CSS/HTML down the wire this way, spending less time processing JS and CSS, and for larger sites it can put a substantial dent in loading time. :)
I just found out a small npm package that implements a Provider and a Consumer for breakpoints. Basically the idea exposed by adamz4008:
I tried it and it's pretty straightforward. Plus it seems the lightest weigthed option vs using something bigger as material-ui or antd (which are great but would be an overkill if you won't use their components or if you're are already using some component library with no breakpoints).
There's also a very simple tutorial on how to use it here
As a super quick "meta-example", you use its BreakpointProvider to wrap your React application component (top-most) and then inside any child component of the wrapped component, you can use the Breakpoint component to do things like:
<>
<Breakpoint small down>
<MySmallComponent />
</Breakpoint>
<Breakpoint medium up>
<MyBigComponent />
</Breakpoint>
</>
"small" is one of five custom breakpoints provided. "down" means, this, or smaller. Similar logic goes for "medium" and "up", meaning "medium sized or larger screens".
If you want to implement this yourself, you should do a provider component with a listener on the resize event. You can find plenty of examples of how to do that when you search for "responsiveness + react" on google, but here is one from this community :) (check the answer from speckledcarp)