How to use multiple css files for react pages? - reactjs

Im building a react application which has two pages(two react components) so far. My main issue here is that for each component i want there to be an external css file for it, but once there are two css files one files over writes the code in the other. For example, I have a homepage class which has some html code and is being routed to by the App.js file. I want to style my homepage component with a css file called home_style, this works fine if there is only one css file. Once i create another css file for that for my other component, it overwrites the code for the first css file(home_style) and ends up being applied for both my components. I had the idea of having all the code in one css file and set ids for each html component to be able to reference it properly, but i want to know if there is a better way to accomplish this.

Importing different css files into your js file doesn't mean it will only affect this js file only. In React, A css file is global, so you need to encapsulate your css with a parent :
// component1.js
const Component1 = () => {
return (
<div className="component1">
<span className="red">This is the component 1</span>
</div>
)
}
// component2.js
const Component2 = () => {
return (
<div className="component2">
<span className="red">This is the component 2</span>
</div>
)
}
Here I use the same class red in my two components. So in my css, if I want these same classes to do different things, i'll need to encapsulate it (as you said) :
//component1.css
.component1 .red {
color: red;
}
// component2.css
.component2 .red {
background-color: red;
}
Importing my css files or just writing this in a global css file will work, but if you are looking for some other solution, you can ahve a look at Styled components or JSS (These are just examples, there is multiple solutions to manage css in React, like simply using SCSS for easier encapsulation).

Related

How to define a dynamic variable externally with SCSS/CSS Modules using NextJS

I would like the content editor to define the primary colors of the website via Headless CMS,
using NextJS with preferable sass modules. Here is an example of it's purpose to give you an idea of what I'm trying to achieve
// style.module.scss
import fromHeadless './api/MyHeadlessCMS'
$color_primary: ${fromHeadless.primaryColor}
$color_secondary: ${fromHeadless.secondaryColor}
$color_text_body: #000;
.title{
color:$color_primary
}
.paragraph{
color:$color_text_body
}
See ${fromHeadless.primaryColor}
// Mycomponent
import style from 'style.module.scss'
const MyComponent = () => {
return(
<div>
<h1 className={style.title}>Hey hey</h1>
<p className={style.paragraph}>Something good</p>
</div>
)
}
export default MyComponent
So I know it is not possible to import js into a scss file, but I'm just trying find a solution with the outcome of using a dynamic scss/css variable defined outside of the sass file.

Styled components - Correct way of approach

I am using styled components in my project.
Consider the following piece of code
import { Footer, FooterLeft, FooterRight, NavLink } from './footer_styles';
const FooterView = ({ prop }) => (
<Footer className="row">
<FooterLeft>
©Sample company, LLC
</FooterLeft>
<FooterRight>
<NavLink to="#" className="footer-link">Privacy Policy</NavLink>
<span className="separator"> | </span>
<NavLink to="#">Terms & Conditions</NavLink>
</FooterRight>
</Footer>
);
So i have the following questions.
1) Can i use bootstrap classes in styled components like what is shown in the code? Is this the correct approach? If not, how to use bootstrap styles along with styled components?
2) Do i need to create a component for each element in dom? For example, in the code that is shown, there is a span tag with class name "separator" for which the styles are added as follows
export const FooterRight = styled.div`
.separator {
float: left;
}
.footer-link {
margin-left: 0px;
}
`;
Is this approach correct? or
Do i need to create a separate component for separator?
I am a bit confused here. Any help would be appreciated.
You can use bootstrap class for style your component, it is nothing wrong. But it better if you use the React Bootstrap, library optimize for React. For example, the drop-down button you can use bootstrap class because it will use Jquery to execute the animation. But you shouldn't do it because Jquery manipulates the real DOM, React manipulate the virtual DOM so it is not good for performance.
You can read more here: https://reactjs.org/docs/integrating-with-other-libraries.html
The answer still yes, with the class to style the component, should use it to reduce the time for coding, with anything related to Jquery, just use the React Component.
I suggest you use the reactstrap, pretty similar to Bootstrap: https://reactstrap.github.io
Another thing, there is many ways to style component, but I am using CSS module, just need to create a CSS file with add-on module of the file name like this:
styleComponent.css --> styleComponent.module.css
And then import to your project:
import styles from './styleComponent.module.css'
And then you can style your component with normal css:
<div className={styles.separator} > Hello World </div>
In styleComponent.module.css:
.separator{
height: 20px;
background: black;
}
.separator:hover{
background: white;
}
It is more easy to manage your project because every single component it has a CSS file to go with it and the className is unique mean locally, Webpack will automatically convert the className 'separator' to '2djfas_separator' that will solve your problem with naming class in CSS.
Hope it helps a little bit for your project!!!

How to use the <style> balise in React file

For a Quick demo purpose, i need to get the actual <style> balise from a page that already exist.
I know that this is not something to do, it's only for a demo.
I have a huge <style> balise to integer into a react app. I just want to copy paste in and make as little change as possible.
I've seen that :
<style>
{'\
div{\
background-color:red;\
}\
'}
</style>
from here : React JSX Style Tag Error on Render
is working but this need to edit all line of my balise and i don't
have the time for that.
I also tried this :
https://medium.learnreact.com/the-style-tag-and-react-24d6dd3ca974
But unfortunately it's not working :/
I'm looking for a quick solution where i only need to edit the beginning and the end of my <style> balise.
Thanks in advance to the community
You can use a template literal.
const CSS = `
div {
background: purple;
color: orange;
}
`;
const app = (
<div>
<style>{CSS}</style>
Hello, world!
</div>
);
ReactDOM.render(app, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
I understand you have a non react project with its styles and you are making it react, but you want to keep the css. If that is the case, I recommend the following:
1 - get the original tag, place it to a css file, and import it into your project's base html, this way you still have your styles whitout doing anything to react
2 - get the original tag, place it into a css file, and import it using javascript/webpack
import styles from './styles.css'
3 - if you still want to place a style tag in react in a similar fashion, I recommend you placing all the style css using the character ` to define de string in the following way (ECMAScript 2015) (the character ` is not the same than ' and it lets you use several spaces and even variables inside. Defining a string like this is called a template literal):
render(){
let myCss = `
div{
background-color:red;
}
`;
return (<style>
{myCss}
</style>)
}
4 - using "styled components". They are designed to make it easier to use css in react. Although they are intented to style a component, you also can do acopy paste of a whole css style inside one of the components. So, as long as all your react elements are children of the specific styled component you are using, it will use those styles. So you can perfectly do the following:
const styled = require('styled-components'); // or import styled from "styled-components";
let Wrapper = styled.div`
div{
background-color:red;
}
`;
class MyReactProject extends React.Component {
/** your react code **/
render(){
return (
<Wrapper>
{ /* your react code */ }
</Wrapper>
)
}
/** your react code **/
}
using styled components you can do a quick fix for the moment, and it will be easy to do a full styled components migration in the future (you only have to transform each of the base tags of your react project into styled components)
link to styled components

Css file effect on another file in react js

I'm new in front end develop and using react js for develop a website
I have some components js file and same in Css file when I changed some class in Css headers same class in footer got change and it's not true what what's wrong in this file
I render a component in one file in the end
You can use CSS modules for your project CSS Modules.Using this, unique class names are generated.
If you dont want to do this, then u you can use CSS class nesting, for eg:
For Header Component
<div className="header">
<div className="title">Main Page</div>
</div>
use css like this:
.header .title{
color:red;
}
Similarity for other components use css like this
.footer .title{
color:green;
}
This way, your css styles will not get mixed up
The CSS file that you import into React is global, which means that if you import a CSS file into a component, it gets added to the HEAD of the HTML document, (if you have hot module reloading) and naturally affects all the components rendered to HTML.
If you want to scope the CSS that you write to a particular component, you can use a library like CSS Modules

CSS for ReactJS - Referencing a div works fine, referencing a className does not

My CSS files work fine when I just use the element names like this:
div {
background-color: blue;
}
But when I use className to identify that div, the CSS is ignored, like this:
.containerInner {
background-color: blue;
}
Here is the js file, so you can see how I'm using className:
import React, { Component } from 'react'
import styles from './Game.css'
import GameContainer from './GameContainer/GameContainer.js'
class Game extends React.Component {
render() {
return (
<div className={styles.containerOuter}>
<div className={styles.containerInner}>
<h1 className={styles.header}>MEMORY</h1>
<GameContainer />
</div>
</div>
)
}
}
export default Game
Notes:
1. I am using create-react-app.
2. I am not using modular css like sass. I'm just using raw css.
3. This is a problem throughout my entire project, not just a couple files
4. There are no error messages in Chrome, or in the terminal
This is the same issue on Stack Overflow, but it does not appear to have been resolved on the thread, and the comments did not lead me to a solution. CSS class selector styles not being applied in React Project
Any ideas? Thanks in advance
If you want to apply a class name to a class, just apply the name. Class names are strings, not objects;
<div className="containerInner">

Resources