Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS modules not working for react version 16.6.0

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

enter image description here

I know that I have to modify

  • webpack.config.dev.js
  • webpack.config.prod.js

but when I read this article I could not find that code.

like image 415
bizimunda Avatar asked Oct 30 '18 10:10

bizimunda


People also ask

Why is my CSS not working in React?

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.

How do I use CSS modules in React JS?

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.

Does CSS work with React?

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.


2 Answers

* as fixed my problem.

import * as headerStyles from './header.module.scss'
like image 139
gökhan kaboğlu Avatar answered Sep 19 '22 11:09

gökhan kaboğlu


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.

like image 31
pflevy Avatar answered Sep 18 '22 11:09

pflevy