reactjscreate-react-appcss-modules

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

Solution

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