Tips on handling various CSS styles for different layouts in ReactJS

Dealing with two different layouts for the Front End and Admin End has presented a challenge for me. I have been including CSS files in the render function for both layouts, but unfortunately, they conflict with each other.

For the Front End layout:

import React, { Component } from "react";
import {  Switch, Redirect, Route } from "react-router-dom";

import Header from "components/FrontEnd/Header/Header";
import Footer from "components/FrontEnd/Footer/Footer";
import Menu from "components/FrontEnd/Menu/Menu";

class Frontend extends Component {
  render() {
    require("../../assets/fonts/frontEnd.css");
    return (
      <div className="section-frontEnd">
        <Header {...this.props} />
        <div id="main-panel" className="" ref="mainPanel">
          <Switch>
            //Routes Swichting ...
          </Switch>
          <Footer />
        </div>
      </div>
    );
  }
}

export default Frontend;

For the Admin End layout:

import React, { Component } from "react";
import {  Switch, Redirect } from "react-router-dom";

import Header from "components/Admin/Header/Header";
import Footer from "components/Admin/Footer/Footer";
import Sidebar from "components/Admin/Sidebar/Sidebar";

class Dashboard extends Component {
  render() {
    require('bootstrap/dist/css/Admin.css')
    return (
      <div className="wrapper">
        <Sidebar  />
        <div id="main-panel" className="main-panel" ref="mainPanel">
          <Header  />
          <Switch>
            //Routes Swichting ...
          </Switch>
          <Footer />
        </div>
      </div>
    );
  }
}


export default Dashboard;

I am seeking a more effective solution to resolve this issue. I should mention that I am not utilizing a webpack file in my project.

Folder Structure:

https://i.sstatic.net/sAC0U.png

Answer №1

To ensure harmonious layout, create a superclass for each page section and base all CSS on it. This approach minimizes potential conflicts between different page elements.

For instance:

.section-content #main-content{
  // CSS code here
}

.section-sidebar #main-content{
  // CSS code here
}

Answer №2

import React, { useEffect } from 'react';

const LoadAdminStyles = () => {
  useEffect(() => {
    import('./admin.css').then(() => {
      // Admin CSS has been loaded
    });

    return () => {
      // Cleanup or remove the admin CSS if necessary
    };
  }, []);
};

const AdminLayout = () => {
  LoadAdminStyles();

  // Admin layout JSX here
  return (
    <div>
      {/* Admin layout content */}
    </div>
  );
};

export default AdminLayout;

const LoadUserStyles = () => {
  useEffect(() => {
    import('./user.css').then(() => {
      // User CSS has been loaded
    });

    return () => {
      // Cleanup or remove the user CSS if necessary
    };
  }, []);
};

const UserLayout = () => {
  LoadUserStyles();

  // User layout JSX here
  return (
    <div>
      {/* User layout content */}
    </div>
  );
};

export default UserLayout;

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

Even though I included a key prop for each item, I am still encountering the error message stating that every child in a list should have a distinct "key" prop

I have been trying to retrieve data from a rest API and display it as text on my browser. However, I am encountering the following error: index.js:1 Warning: Each child in a list should have a unique "key" prop. Below is how I have set up the key prop. ...

How can we dynamically retrieve an image from the database in React in order to display it on the user interface?

I have a React front end with Node backend and I'm using sequelize for mySQL. Within my database, there is a table called "company" with a column named "logo" where images are stored. How can I dynamically generate a URL for the img tag in order to di ...

What is the most effective method to indicate "justify" for a grid item in material-ui?

I am working with a basic Grid component from the popular material-UI library. Here is my current setup: <Grid container> <Grid item sm={6}> <SearchPanel/> </Grid> <Grid item sm={6}> <ItemStatus ...

What is the process for importing types from the `material-ui` library?

I am currently developing a react application using material-ui with typescript. I'm on the lookout for all the type definitions for the material component. Despite attempting to install @types/material-ui, I haven't had much success. Take a look ...

Changing the color of a specific IconButton upon clicking in ReactJS

Exploring React JS for the first time. I have a scenario where I am working with multiple IconButtons and want only the clicked button to change its color when clicked. Currently, using state causes all buttons to change color simultaneously. What is the b ...

Tips for defining boundaries for position absolute elements using JavaScript

Fiddle. I am in the process of developing a chat/mail application with a resizable menu similar to the Google Hangouts desktop app. When resizing the brown content pane at a normal speed, you can observe that the boundaries are functioning as intended. My ...

What is the best way to incorporate a variety of colors into a single element?

Using a combination of colors in one element I'm looking for ways to incorporate multiple colors into a single element effectively. My initial attempt with the color mix function didn't work as expected, so I need new ideas and techniques on how ...

The combination of two tags can create a powerful and impactful presence

My task is to place two <h3> tags in the same line or paragraph. <h3>This is the first paragraph. </h3> <h3>This is the second paragraph.</h3> I would like the output to look like this: This is the first paragraph. This is Se ...

React: Blur input element when escape key is pressed

I need help with a functionality for an input tag. When the escape key is pressed, I want the focus to be removed from the input element so that there is no text cursor or any styling related to focus (meaning user input will not be registered in the input ...

Ensure that the text is wrapped properly when implementing innerHTML functionality

Within my Angular 5 application, I am faced with a requirement to display HTML content retrieved from a database. An example of the text stored in the database is: <div><u>Documents and Files</u></div><ul><li>These docu ...

What is the method to retrieve text from a div element with Webdriver-IO?

Is there a way to extract the value from the following HTML element using Webdriver-IO for automated testing? <div class="metric-value ng-binding" ng-style="{'font-size': vis.params.fontSize+'pt'}" style="font-size: 60 ...

Is it possible to create a button function in HTML that can alter the CSS image tag?

Is there a way I could create a function that can retrieve a value, update an image source, and then incrementally select another value? It would be incredibly helpful if you could provide a reference where I could learn how to do this or explain it in det ...

Error alert: Blinking display, do not dismiss

I am trying to make it so that when the "enviar" button is clicked, the "resultado" goes from being hidden ("display:none") to being visible ("display:block"). This is my html document: <script type="text/javascript"> function showResu ...

Placement: fresh column in absolute position

Could someone please help me understand this situation? I have posted my code on jsfiddle. I am using Bootstrap 4. In my design, I have text placed over an image with the parent div having position:relative. The left div has position:absolute and left:2%, ...

Tips on disregarding events within an html table

I have a see-through table with a width set to 100% that houses various HTML content. I am utilizing the table to properly center a particular element on the screen. However, the table is intercepting mouse events and preventing users from clicking on lin ...

Include a new item into the existing one and iterate through the information within it

Is there a way to iterate through the compositions array within the sample object and then use that data to fill the compositions array of the objToAdd object? const sample = { lin: { "clo": [ { "mode": 19, "id": ...

Unable to transfer props object to React component using TypeScript

Apologies if this seems like a basic issue, but I've been struggling with it for the past two days. I'm currently working on writing a simple unit test in Vitest that involves rendering a component to the screen and then using screen.debug(). The ...

I am having trouble getting the (:active) pseudo-class to function properly with the menu on my WordPress theme

Purchased a theme called HelpGuru and encountering issues with the CSS of the menu. Reached out to support but they were unable to assist, directing me to a company that specializes in customizing WordPress themes instead. The link to the demo can be found ...

The skipping of crucial text in tab focus is adversely affecting the accessibility of the website

My appbar contains an image link, a warning indicating the user's current environment, and details about the logged-in user. While testing accessibility with a screen reader, I noticed that tab focus skips over the user details in favor of a link in ...

What is the best method for performing a basic POST request in NextJS 13.4 API with the new App Router, utilizing Response, NextAPIResponse, and NextResponse?

How can a simple POST request be made in NextJS 13.4 using the new App Router? The traditional method of using NextAPIRequest, NextAPIResponse, checking for 'POST' method with if (req.method === 'POST'), extracting parameters from the ...