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?

13
  • try changing style to style-loader Commented Jun 5, 2017 at 12:37
  • It Didn't work. Commented Jun 5, 2017 at 12:40
  • maybe your loader is not appropriate.test after changing your loader Commented Jun 5, 2017 at 12:41
  • try this { test: /\.css$/, loader: 'style-loader!css-loader'} Commented Jun 5, 2017 at 12:48
  • This won't activate the module mode of css loader. This is just a css loader. Commented Jun 5, 2017 at 12:53

2 Answers 2

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>
    )

};
Sign up to request clarification or add additional context in comments.

Comments

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 [email protected]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.