I just went through this, and seems that there are lot of people lost or not finding the right answer. This is what I did:
Get control over the configuration
npm run eject
Running this command will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project, as you noted all that was inside a npm module called react-script. Do not modify this package directly!
Install sass compiler and webpack loader
npm install sass-loader node-sass --save-dev
At this point you have to install the sass compiler and the webpack loader as development dependencies.
Modify the webpack configuration
- Open
config\webpack.config.dev.js. Add
/\.scss$/,to the exclude array in the url loader, it will look something like this after the update:exclude: [ /\.html$/, /\.(js|jsx)$/, /\.css$/, /\.json$/, /\.svg$/, /\.scss$/, //Add this line ],Add the SASS/SCSS loader:
{ test: /\.scss$/, loaders: ['style', 'css', 'sass'] },
I just went through this, and seems that there are lot of people lost or not finding the right answer. This is what I did:
Get control over the configuration
npm run eject
Running this command will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project, as you noted all that was inside a npm module called react-script. Do not modify this package directly!
Install sass compiler and webpack loader
npm install sass-loader node-sass --save-dev
At this point you have to install the sass compiler and the webpack loader as development dependencies.
Modify the webpack configuration
- Open
config\webpack.config.dev.js. Add
/\.scss$/,to the exclude array in the url loader, it will look something like this after the update:exclude: [ /\.html$/, /\.(js|jsx)$/, /\.css$/, /\.json$/, /\.svg$/, /\.scss$/, //Add this line ],Add the SASS/SCSS loader:
{ test: /\.scss$/, loaders: ['style', 'css', 'sass'] },
I was stuck with the same problem reading it's documentations.
Unitl I found that the README.md file inside the created project with create-react-app has documented it in another way and it worked. I think relying on that readme file ( that is shipped with your created project ) is better option.
Hello, React Newbie here
I was trying to add SCSS on a React App by adding import './index.scss' which the scss file was supposed to change the h1 text color to blue but it didn't style anything instead the text color was still black and when i change the index file extension to css, the text color goes blue.
Any help is appreciated.
Notes:
Yes, I have Sass installed on npm (the Dart version to be specific)
Hi,
So i've recently created a new react app using
$ npm create-react-app $ npm run eject $ npm install sass-loader style-loader css-loader --save-dev
I changed the default .css files to be .scss, and changed the default imports to be importing the '.scss' file instead of a '.css' file.
My webpack.config.dev.js looks like this: https://gist.github.com/VirtuallyBookish/9a0e851268061d9e5325ebe872f5c91e
But whenever I attempt to run the app using:
$ npm run start
I'm greeted with an error like this (or similar): ./src/index.js Module not found: Can't resolve 'style' in '/Users/usr_name/Sites/react/app-demo'
I was following this tutorial for adding it: https://medium.com/@gpickett00/didnt-work-for-me-22e05f97b5a7
I've been struggling on this for a while, so any help would be massively appreciated! Thank you!
EDIT:
I sorted this out I think! I changed the line that read:
loaders: ['style', 'css', 'sass']
to
loaders: ['style-loader', 'css-loader', 'sass-loader']
Hello, this is my first time working with React and I'm running into some issues.
I created a CSS file but it is not applying the styles when I use className or id. However it is applying the style when I use only
the html elements (i.e. h1, input, ul, etc).
I saved the css file in the src folder and wrote the import statement so I am very confused as to what's going on.
Please let me know if further information is needed and thank you in advance!
According to the React Static Boilerplate website, they use CSS Modules - this would explain why the body tag is being respected but the class selector is not.
https://github.com/css-modules/css-modules
try importing the stylesheet like so:
import styles from './MyComponent.css';
The using it in your component like so:
<div className={styles.card}>something!</div>
I had an issue with CSS not being applied, but it wasn't the modules, it ended up being the syntax of applying multiple classes. The syntax below solved the problem
<div className={[styles['col-sm-16'], styles['col-md-10'], styles['col-lg-8']].join(' ')}> some content </div>
Sass is not recommended to use today for global styles, because as they wrote -> it can generate more files than you want -> effect is that you have worse performance
This information we can read on their main page
I recommend you use once simple CSS file for variables, global styles then easily you can reuse them and if you wanna use Sass as a developer then go-ahead for the rest of styling
Best!
For example what you can do
App.css:
body {
margin: 0;
font-family: Lato, Helvetica, Arial, sans-serif;
transition: all 0.3s linear;
}
:root {
--facebook-color: #507dc0;
}
And then:
Login.scss
body {
.login {
background-color: var(--facebook-color);
}
}
Of course this is only example.
In a react project created with create-react-app or npx create-react-app, I also had the same issue.
I had imported index.css file in my App Component but my styles were not being applied properly to my React Components.
I even tried directly adding index.css file in my html file in the public folder and using link tag to link to my index.css file (which resides within src folder).
<link rel="stylesheet" href="./../src/index.css">
That also didn't work.
Finally, I read an article about 7 ways to apply CSS into React. One best way was to install node-sass into our project and use index.scss ( import './index.scss') into App Component instead of index.css.
And Hurray!!! My CSS worked fine, All the Media Queries started to work fine.
Below is the code snippet you can try.
import React from "react";
import ReactDom from "react-dom";
import './index.scss';
// --- Data to work with ---
const books = [
{
id: 1,
name: 'The Rudest Book Ever',
author: 'Shwetabh Gangwar',
img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
},
{
id: 2,
name: 'The Rudest Book Ever',
author: 'Shwetabh Gangwar',
img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
},
{
id: 3,
name: 'The Rudest Book Ever',
author: 'Shwetabh Gangwar',
img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
},
{
id: 4,
name: 'The Rudest Book Ever',
author: 'Shwetabh Gangwar',
img: 'https://m.media-amazon.com/images/I/81Rift0ymZL._AC_UY218_.jpg'
},
];
const Book = ({ book }) => {
return (
<div className={"book"}>
<img src={book.img} alt="book image" />
<h3>{book.name}</h3>
<p>{book.author}</p>
</div>
)
};
const Books = () => {
return (
<main className={"books"}>
{
books.map(book => {
return (<Book book={book} key={book.id} />)
})
}
</main>
)
};
// Work a bit fast | one step at a time
const App = () => {
return (
<main>
<h2>Books</h2>
<Books />
</main>
)
}
ReactDom.render(<App />, document.getElementById("root"));
/* --- Mobile First Design --- */
.books{
text-align: center;
};
.book{
border: 1px solid #ccc;
text-align: center;
width: 200px;
padding: 1rem;
background: #001a6e;
color: #fff;
margin:auto;
};
h2{
text-align: center;
}
/* --- Adding Media Queries --- */
@media only screen and (min-width: 900px){
.books,.persons{
display:grid;
grid-template-columns: 1fr 1fr;
}
}
To install node-sass, simple do npm install node-sass --save
Then rename all your .css files with .scss and your project with work properly.
The package.json should have the node-sass dependency added as shown below:
"dependencies": {
"node-sass": "^4.14.1",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-scripts": "2.1.5"
},
Hope this will help many developers :)
It would be helpful to see your React component as well.
Given this code, you are passing className as a property into the component rather than assigning a class to it:
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
data: null
};
}
render() {
return (
<div>
/** This line specifically **/
<ContactList className="contactList" />
<ContactDetail />
<AddContactModal />
</div>
);
}
}
Inside your component, you would need to use className={this.props.className} on a regular HTML tag inside of your component in order to pass the className through.
Your webpack configuration may be changing the class name depending on its settings. (CSS Modules enabled, though your comment about stylesheet intact implies it's not).
Since you are importing the './style.css' file, try referencing the class name like: style.foo. Ex:
<div className={ style.foo }>foo div</div>
This article: http://javascriptplayground.com/blog/2016/07/css-modules-webpack-react/ describes the pattern of importing and referencing the class name pretty well.
If you're like me and use the style attribute for styling then afterwards move them into proper classes in .css files (and wonder why it doesn't work), remember to remove the JSX string format,
height: "100px"
is not the same as
height: 100px