Is there any method of importing .css files conditionally in React.JS? - css

I'm trying to add a dark-mode in my project. Without React, I would have just targetted the DOM element which triggers the switching of the mode and linked my custom dark.css file instead of index.css which happens to be my pre-dominant CSS file. But here in React, it would take the latest file I'm importing for my CSS. I'm a beginner in React. So please pardon me if this doesn't make much sense. Thanks in advance!
Here's my code
import ReactDOM from 'react-dom';
import './index.css';
{/*import './dark.css*/}
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

Yes there is a way. You can use React.Lazy and React.Suspense.
For example this article explains how use them.

There's nothing wrong with importing dark.css every time, but its styles should be applied conditionally. What I mean is, all the styles for dark mode should be wrapped in a CSS class like .dark-mode. Then, when user turns dark mode on, you would apply this class to body tag or to the top-level React component, and remove it when dark mode is turned off.
This will be a much easier solution that trying to work with lazy loading.

For anyone viewing this, I would strongly recommend using useContext. It makes it very easy to pass your theme without using props to all of your children. They even have an example of implementing dark/light theme like you want: https://beta.reactjs.org/reference/react/useContext#usage

Related

Limiting CSS framework to Vue scope

I got a problem with the tailwind preflight. It's resetting some styles globally, which I really don't want it to. And disabling preflight would be a huge amount of work.
So, is there a way to use a css famework just inside the Vue.js scope without it affecting other styles of the page (I'm using Vue on top of an existing project)?
Importing it as usual:
import { createApp } from "vue";
import ImportConfig from "~/components/data_import/import_config/ImportConfig.vue";
import "~/styles/main.css";
Won't do the trick, cause the styles are accessible globally.
Also using VUe's custom-components ain't a proper solution cause these are using the shadow dom.

CSS styles aren't applying in my React app

When I add new HTML code to my React app , the CSS styles from the App.css file aren't applying anymore , until I retype Import './App.css'" in the head.
Any solution please.
All our components in React act like modules and have their data (variables, functions) private to them. To access code or styles from other files we need import from other modules.
Our App.js is also one such component. Until you do import './App.css', the CSS will not be applied to App.js.
Problem solved , it was about bootstrap .
Import bootstrap/dist/css/bootstrap.min.css before App.css .
I was doing the opposite .

Is there a way i can make an imported CSS library affect only specific complements or elements in React JS

In my React Application,i'm using React-Bootstrap and i've imported
https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css
In order to style some other components, i imported MDBreact library, when i did so,the components (that were styled using bootstrap.min.css) have been effected by the exist of MDB library.
Any suggestion to resolve this issue would be appreciated
Theorically, just import what you need where you need it ? You usually import bootstrap in your main component, but if you want it only in a child, nothing prevent you from importing it here.

What is the difference between import css or link css in a React app?

In a React app I can include my CSS either using import or <link> it in the index.html file (From a CDN, for example).
What is the difference?
Is there any significant performance difference between the two methods?
To be clear, I mean this type of import from inside a *js or *.jsx file:
import React, { Component } from "react";
import "bootstrap/dist/css/bootstrap.css";
In theory, the only difference between them is that import is the CSS mechanism to include a style sheet and the HTML mechanism. However, browsers handle them differently, giving a clear advantage in terms of performance.
Also, using the tag allows you to define "preferred" and alternate stylesheets. You can't do that with import.
Overall, the tag is processed more quickly than the import rule (which is apparently somewhat slow as far as the css processing engine is concerned).

React - Child componet stylesheet overwriting other child's stylesheet

I'm trying to apply separate styleSheets for every child component by importing different styleSheets in different components but fails to achieve this as styles are being overwritten.
Sample Code: Stackblitz
childa.jsx:
import React from 'react';
import "./childa.css"
export default () => <h1>Child A!</h1>;
childa.css:
h1 {
color: blue;
}
childb.jsx:
import React from 'react';
import "./childb.css"
export default () => <h1>Child B!</h1>;
childb.css:
h1 {
color: red;
}
This is just a sample code. Need solution for a project having large styleSheets.
Based on your clarification in one of your comments:
The thing is I'm converting a project from angular to react and all
the css is already written so I can't use inline style. Is there any
way in which I don't have to rename all the css classes in all the
stylesheets?
Short ans: You can't achieve that as of now.
This article explains all the different ways to style react components. In your case, the best that you can do is use css modules and rename generic classes like h1 to .h1.
Check this great article about css modules: Modular CSS with React.
Note: css modules are not available in create-react-app. If you must use it here's an
article on how to use CSS Modules with create-react-app.
I think this is caused by ther order of the imports.
In your parent component you have something like
import React from 'react'
import ChildA from './ChildA'
import ChildB from './ChildB'
This means that in the compiled code you'll have the two stylesheets imported one after the other, and the second h1 rule will overwrite the first
You should use classes for your components, or use inline style
Importing a css does not wrap it in the scope of the component is just a straight import into the DOM. In order to mantain a separation of components styles you have to approach with another solution, as styled-components.
This may not work for your entire application, but I fixed it by applying a class to the element (.childA and .childB). This solved the problem.
export default () => <h1 className='childB'>Child B!</h1>;

Resources