0

I am trying to use css module in my reactjs application. I have added the loader in webpack.config file.

 {
                test: /\.css$/,
                loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'

            },

But when I import a css file in my application and try to use it for classNames like {styles.example} then it does not link to .example in my css file.

This is how I am doing it:

import styles from './TestPage.css'

<div className={styles.example}></div>

This is not working.

Components are not styled. I am not getting any error in console though. How can I solve it?

Ricardo Sanchez
  • 4,935
  • 11
  • 56
  • 86
EdG
  • 2,243
  • 6
  • 48
  • 103

2 Answers2

6

You need to Rename the file

from filename.css to filename.module.css

Usage:

login.module.css

.login {
    background-color: red;
}

Login.js

import styles from './login.module.css';

const login = (props) => {
    return (
            <div className={styles.login} >
                Login Card
            </div>
    )

};
Jawla
  • 333
  • 2
  • 11
1

Your problem might be because of old react-scripts version.Just update your react-scripts version more than 2.0.3.

npm install --save-dev --save-exact react-scripts@2.0.3
vimuth
  • 5,064
  • 33
  • 79
  • 116