Kirill Ovcharenko
Kirill Ovcharenko

Reputation: 53

CSS Modules class names undefined

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;
}

Upvotes: 5

Views: 7112

Answers (3)

Janspeed
Janspeed

Reputation: 2814

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.

Upvotes: 0

Dave Lee
Dave Lee

Reputation: 516

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

Upvotes: 0

Abdelkarim EL AMEL
Abdelkarim EL AMEL

Reputation: 1533

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';

Upvotes: 10

Related Questions