5

I try to use css modules in my project. I'm using latest Create React App with react-scripts version 2.1.8. In the docs it says I can add CSS Modules, and i dont have to do eject script.

I tried to do the same way, but my styles are empty and classes are undefined. Here is the code for one of my components:

import React, { Component } from 'react';
import styles from './Catalog.css';

class Catalog extends Component{

render() {
    console.log('styles.catalog '+ styles.catalog); //returns: styles.catalog undefined.
    return (
      <div className={styles.catalog}> //returns: <div>
        ...
      </div>
    )
}

Catalog.css:

.catalog {
  min-width: 800px;
}
1
  • Did you double check the path of your css module ? Commented Apr 7, 2019 at 18:01

3 Answers 3

10

In the docs, it's mentioned that you have to name the file as follow :

[name].module.css

Try renaming the file to : Catalog.module.css and import it like this :

import styles from './Catalog.module.css';
Sign up to request clarification or add additional context in comments.

1 Comment

If you are using typed-css-modules also you have to use .module.css
0

It's also not recommended to use like catalog-v2.module.css. Instead it'd work if the name of the file is like catalogV2.module.css

Comments

0

Adding this answer in case someone else ends up with the same problem as me. My problem was a case sensitivity issue. Apparently the styles still worked when I loaded the file "../inputTextField.scss" when it should have loaded with uppercase I from "./InputTextField.scss"

Somehow it still "cached" the styles or something because the main style worked when doing changes but if I added a style to the component like "style.inverted" and then added that class to the scss file it didn't recognise it and it became undefined.

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.