React application with a layout design that adjusts and responds to various

One task is to display various components based on the device's width;

Two possible approaches have been considered:

  1. 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.

  1. 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?

Answer №1

 @media only screen and (min-width: 768px) {
  section.dashboard .slick-list .slick-track {
    display: flex;
  }
  section.dashboard .slick-list .slide {
    opacity: 1;
  }
  header .wrapper .article h1 span.arrow {
    display:none;
  }

  header .wrapper .article .description {
    max-height: 300px
  }
} 

@media only screen and (min-width: 1024px) {

  .container header .wrapper {
    text-align:left;
    margin-left:5%;
    width:480px;
  }

  .container header .header-nav-area #nav_container  {
    display:flex;
  }

  .container header form {
    display:block;
  }

  .container header .menu-icon {
    display:none;
  }

  header .wrapper .article footer {
    display: block;
  }

  section.dashboard .slick-list .slick-track {
    display: flex;
    min-width: 309px;
    padding: 20px;
  }
  
  section.dashboard .slick-list .slick-track[index="2"] {
    display: flex;
  }

  section.dashboard .slick-list .slide {
    opacity: 1;
  }
} 

For more information, please take a look at:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React JS: Event bubbling occurs even without any event listeners attached

Take a look at this demo code snippet: function AComponent() { return ( <div style={{ width: '150px', border: '1px solid red' }} onClick={() => { console.log('container 1 called'); }}> <div style={{ wi ...

Is there a term similar to "Rise above the Rest" in the world of Web Development?

Hey, I've encountered a new issue right now. Currently, I have two elements that are fixed to the top and bottom of the page. However, the elements in between them are overlapping the top element. Even though I tried keeping both elements fixed, th ...

Unable to load nested iframe

When working with an HTML document, I tried to add an iframe in the body without any cross-origin restrictions (same URL). However, when I tried to do the same thing within the appended iframe, although the nested iframe element was successfully added to ...

What is the functionality of the Material-UI theme light and dark mode feature?

I am having trouble understanding how the light/dark feature in this theme operates. I have noticed that changing the main color affects the components, but adjusting the light/dark value in type does not seem to have any effect. const customizedTheme = ...

An effective solution to address the Eslint warning regarding the prohibition of spreading specific props

Just getting started with React and I have a question. How can I address the Prop spreading is forbidden Eslint warning that keeps popping up? After reading through the Eslint documentation, it seems like I have to destructure all props like this: con ...

How can we add up the sum with just one click and then display the

Is there a way to calculate numbers within specific DIV boxes and display the total in a separate DIV when a box is clicked? I've attempted different methods, including enclosing the code within the window.onclick function. Check out my code on this J ...

The best way to close a swipeable drawer from a different class

I'm feeling a bit puzzled by the explanation of SwipeableDrawer on the Material-ui website. Here's the setup I have: there's a Component called 'Sidebar' that opens a SwipeableDrawer when a user clicks a button on the appbar or swi ...

How do I efficiently render multiple form field components in React CSS when two of them have different flex directions?

I need to find a way to dynamically iterate through multiple input components. All inputs follow the same style pattern except for 'first name' and 'last name', which should be aligned in one row with different widths. <div style={{f ...

How can you center a webpage and make it occupy 60% of the screen?

I was attempting to design a website where the main content takes up 60% of the body. This is what I tried initially: body { align-items: center; text-align: left; justify-content: center; width: 60%; } Unfortunately, this approach did not work ...

What advantages does using the useQueryClient from the react-query library offer?

The react-query quick start documentation, available here, provides an example code snippet as shown below: // Create a client const queryClient = new QueryClient(); const App = function () { return ( // Provide the client to your App <QueryC ...

Adjusting the content of a single text box by typing in another

Is it feasible to automatically convert a Nepali date input in one textbox into an English date and display it in another textbox without any page refresh? I have a PHP function that can translate dates between Nepali and English, and I want it to execute ...

Error: Unable to access property 'count.' because it is undefined

componentDidMount(props) { this.interval = setInterval(() => { if (props.count !== 0) { this.stateHandler() } }, 1000) } Encountering an issue with the interval, as the console is displaying the following error: Type ...

Fill a form with jQuery and ajax data after it has been submitted

I'm working on a simple HTML form and I want to use Ajax to perform a lookup using a PHP file after entering data into the first field. The goal is to fetch information from an external source for the two remaining fields. <form method="post" acti ...

Can we divide an animation in Three.js according to a model's individual parts?

Recently delving into the world of three.js, I have encountered a project with specific requirements: Load a humanoid gltf model Play various animations on the model Stop or play animation for only the head part of the gltf model without altering animatio ...

Show the HTML page returned by the server on the client side

Hey there! I've come across a PHP code snippet that sends back an HTML file as a response. This PHP code makes use of the include method to send the file. When I'm on the client side, I'm employing AJAX. Upon typing console.log(data) (with ...

Ways to showcase the content of a page once the controller variables have been set

As someone who is just starting out with AngularJS and web development, I am curious to know if there is a way to delay the display of a page until after all of the controller's $scope variables have been initialized. Most of my $scope variables are c ...

Is there a way to verify whether a user is currently logged in? (Using everyauth in node.js)

Previously, I relied on client-side auth for my application. Recently, I integrated server-side everyauth and it's functioning well. However, I'm unsure how to perform a function similar to FB.getLoginStatus (which I used in the client-side) when ...

Determine the central point of the cluster with the most concentrated xy data points

In my project, I have a 200 x 200 square grid where users can vote on the next desired position of an object within this space. Each time a user submits their vote, the XY position is added to an array. Currently, to determine the "winning" position, I ca ...

What is causing the ng-show function to malfunction after building with phonegap?

My current javascript framework for my PhoneGap app is the Ionic Framework. I have a specific item on one of my pages that I want to make expandable and collapsible by clicking an anchor on the page. Here is what the code looks like: In the HTML file: & ...

The Express JS is having trouble serving the static CSS files through the router

Currently, I am encountering this issue in a larger project, so I have created a simplified version to explain it more clearly. The Tree Structure of my project is outlined below: demo |____admin | |____admin.js |____node_modules | |____static | ...