I was trying to use CSS Modules in React.
Here my code of App.js
import React from 'react';
import styles from './index.css'
const App = () => {
const REACT_VERSION = React.version;
return (
<div>
Main app
<div style={styles.test}>Green</div>
<div>Yellow</div>
<div>React version: {REACT_VERSION}</div>
</div>
);
};
export default App;
Here is my code of index.css
.test {
background: black;
color: white;
font-size: 34px;
border: 34px;
}
Here is the output
I know that I have to modify
but when I read this article I could not find that code.
This error is generated because the compiler is only able to import files from the src folder. Here, the CSS file is saved outside the src folder, so the compiler failed to import it. To make this code work, you just have to save the CSS file inside the src folder.
To import a CSS Module into the corresponding component, import the css modules stylesheet as styles or [name]Styles : In JSX, use the defined CSS class as a className prop like so: From here, you can add as many other CSS modules you'd like, just remember to import each as a different name.
As the name suggests, React can import CSS files. The process is similar to how we link up CSS file in the HTML <head> : Create a new CSS file in your project directory.
* as
fixed my problem.
import * as headerStyles from './header.module.scss'
I had this same problem and solved it by renaming my css file to:
myName.module.css
and also importing like this:
import styles from './myName.module.css'
There is no need to follow the steps of updating the webpack files any more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With