One task is to display various components based on the device's width;
Two possible approaches have been considered:
- Create a React Component that accepts multiple components for different widths (sm, md, xl). It will automatically determine the device's width and render the appropriate component. (example)
<DeviceChecker>
<Desktop>
<List/>
</Desktop>
<Mobile>
<Carousel/>
</Mobile>
</DeviceChecker>
One downside of this approach is the need to constantly check the width on window resize events.
- Develop both components in React, but use CSS media queries to show or hide each, like so:
<div>
<Carousel className="sm" />
<List className="md" />
</div>
One drawback of this method is that React will still render both components, even if one is simply hidden.
While both variants can be implemented, the question remains: Which approach is the correct way to create Responsive Layouts for React Applications?