Using CSS files that are not properly imported into React components

Currently, I am utilizing multiple CSS files for a specific purpose. The issue I am facing is that when I apply styles to one component in a CSS file, it inadvertently affects other components as well, not just the intended one.

I believe the root of this problem lies within react-router. It seems to be sharing CSS files across all components imported into it. When a component is no longer used in the router, its associated CSS files are no longer shared.

But how can I approach routes differently? I need to import all components into the router in order to have links to my pages.

I even attempted placing routes in separate files, but that did not resolve the issue either.

This is what my route file structure looks like:

import React, { Component } from 'react';
import {
  BrowserRouter as Router,
  Route
} from 'react-router-dom';

import './App.css';
import LoginPage from '../pages/LoginPage.js';
import Navbar from './Navbar.js';
import MainPage from '../pages/MainPage.js';
import SportsfieldsListPage from '../pages/SportsfieldsListPage.js';
import MyProfilePage from '../pages/MyProfilePage.js';
import SingleObjectPage from '../pages/SingleObjectPage.js';
import ConfirmPage from '../pages/ConfirmPage.js';
import AdminPage from '../pages/AdminPage.js';

class App extends Component {
  render() {
    return (
      <Router>
        <div className="container">
        <Navbar />
          <Route exact path="/" component={MainPage} />
          <Route exact path="/listaBoisk" component={SportsfieldsListPage} />
          <Route exact path="/LoginPage" component={LoginPage} />
          <Route exact path="/MyProfilePage" component={MyProfilePage} />
          <Route path="/object/:id" component={SingleObjectPage}/>
          <Route path ="/confirm/:id/:value" component={ConfirmPage}/>
          <Route path ="/adminPage" component={AdminPage}/>
        </div>
      </Router>
    );
  }
}

export default App;

Answer №1

If you're looking to keep your CSS styles organized in your project, consider using CSS modules. With CSS Modules, unique class names are automatically generated for each component.

Alternatively, if you prefer not to use CSS Modules, you can utilize CSS class nesting. For example, for the Main Page Component:

<div className="main-page">
 <div className="title">Main Page</div>
</div>

Style the component using CSS like this:

 .main-page .title{
  color:red;
 }

Apply similar styling techniques for other components by nesting CSS classes accordingly:

 .login-page .title{
  color:green;
 }

This method ensures that your CSS styles remain separate and do not get mixed up.

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

Getting dynamic props from a clicked element in React involves accessing the target element's properties and

I am currently working with a React "tree" menu component that has main links with submenus generated dynamically through a JSON GET call. Upon inspecting the tree in the React Inspector, I noticed that each element has multiple props associated with it. H ...

Struggling to implement JQuery code in a Next.js application

I'm having trouble getting my tracking code to work in Next.js. <Script> window.dataLayer = window.dataLayer || []; function gtag(){ dataLayer.push(arguments) } gtag('js', new Date()) ...

"Enhance your website with a backspace button using jquery - here's

Recently, I delved into the world of jQuery and decided to test my skills by creating a jQuery calculator. Everything worked perfectly except for the backspace button. This is what I tried: if (value === backspace) { $(input).val($(input).val().substring ...

Changing the background of one div by dragging and dropping a colored div using jQuery

Looking at my HTML code, I have 4 <div> elements - 2 represent doors and 2 represent colors based on their respective id attributes. My goal is to enable users to drag any color to either door (e.g. blue on the left door and black on the right) and ...

What was the reason behind the unsuccessful initiation of the Node.js instance in the Google App Engine standard environment?

After deploying my nextjs project to Google App Engine standard environment, I encountered a 500 error. I found inspiration from this discussion and structured my app based on this repository. The demo project deployed successfully confirmed that it was wo ...

Learn how to effectively transfer parameters between two React components

I am currently working on a component that looks like this: pays11 = iconAllemagne; pays12 = iconHungrier; score11_12='2 - 2'; bonus11_12 = 'x0'; render() { return ( <Page> ...

What is causing the need for me to tap TextField twice just to see my list pop up?

I recently updated from @material-ui/[email protected] to @material-ui/[email protected]. After the update, I noticed that all my TextFields require two clicks for the lists to display. Interestingly, reverting back to version v4.9.1 does not sol ...

What is the appropriate syntax for the second division?

<style> .panel{ border:1px solid #000; } </style> <div class="body"> <div class="panel"></div> <div class="panel" style="border-top:0px;"></div> </div> In order to apply the style border-top:0px; to ...

Manipulate and transform elements within an array using both Filter and Map functionalities in React

Below is the component I am working with: function Params(props) { const { Parameters } = useFetchParams(); return ( <div className='Params'> { Parameters && Parameters.map(parameter =&g ...

Utilizing React Query with JSON-ready data formats

I've recently started using React query and came across the following information: According to React Query, structural sharing is only effective with JSON-compatible values. Other types of values will always be considered as changed. If you're ...

How to avoid clipping in Bootstrap 4 when a row overflows with horizontal scrolling

In order to allow for horizontal scrolling of all contained columns, I created a row using the row class. To implement this feature, I followed advice from a contributor on Stack Overflow. You can view their answer by following this link: Bootstrap 4 hori ...

Understanding the proper way to enclose JSX components and exhibit JSON based on user input

I'm currently working on a university administration website using React that needs to support multiple languages. I have successfully built the Login page, which you can see here: https://i.stack.imgur.com/cIiaU.png Now, my next task is to allow us ...

The layout of my website is perfect when my laptop's zoom is set to 90%, but at 100% it starts overflowing

Currently, I am in the process of building a website on my laptop while following an instructor's video tutorial. It has been important for me to set the same pixel measurements as the instructor in order to replicate the design they provided. However ...

Guide to adding multiple class names to an HTML element while utilizing SCSS in React

How can I add multiple class names from a styles module to one HTML element in React? Here is the code snippet: /styles/styles.module.scss file .gridContainer { display: grid; gap: 2em; grid-template-columns: 1fr 25em; grid-template-rows: auto; } ...

Customizing Material UI Select for background and focus colors

I am looking to customize the appearance of the select component by changing the background color to "grey", as well as adjusting the label and border colors from blue to a different color when clicking on the select box. Can anyone assist me with this? B ...

Creating a custom loading page in Next.js 13: A step-by-step guide

Hello, I am currently working on implementing a loading page for my website to enhance the user experience during a longer loading time. I have created a simple functional component that displays a loading message and imported it into my layout.jsx file in ...

Immersive image display

Currently on my website, I have a table displaying 9 images with descriptions. I'm looking to enhance user experience by allowing them to click on an image and view it in a larger format like a gallery without disrupting the layout of the page. This ...

The error in ReactJS is coming from the render method of the `News` component

While working with React JS, I encountered an error when trying to run a particular code snippet. The error message displayed was: "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got ...

Discovering vacation days in the Persian calendar (Shamsi)

How can I access Persian calendar holidays in React without using paid APIs? Is there a free way to get information about Persian (shamsi) holidays, including descriptions? { 1402:[ { date: "1402/1/2", description: "عیدن ...

An ideal loader may be necessary to manage this particular file format

After using npm publish to create a package and then installing it in a new project, I encountered the following error: > ./node_modules/react-js-validator/src/inputs/EmailInput.js Module parse failed: Unexpected token (38:12) You may need an appropria ...