0

I ejected my create-react-app in order to use css modules. I modified my webpack.config.dev.js from

  {
            test: /\.css$/,
            use: [
              require.resolve('style-loader'),
              {
                loader: require.resolve('css-loader'),
                options: {
                  importLoaders: 1,
                    modules: true,           //added this
                    localIdentName: "[name]__[local]___[hash:base64:5]"  //added this
                },
              },
              {
                loader: require.resolve('postcss-loader'),
                options: {

                    // Necessary for external CSS imports to work
                  // https://github.com/facebookincubator/create-react-app/issues/2677
                  ident: 'postcss',
                  plugins: () => [
                    require('postcss-flexbugs-fixes'),
                    autoprefixer({
                      browsers: [
                        '>1%',
                        'last 4 versions',
                        'Firefox ESR',
                        'not ie < 9', // React doesn't support IE8 anyway
                      ],
                      flexbox: 'no-2009',
                    }),
                  ],
                },
              },
            ],
          }

Then I tried to uss css modules in one of my components

import React, { Component } from 'react';

import styles from './sidebar.css';

class MenuPanel extends Component {
    render() {
        return (
            <div>
                <div className={styles.navbarSide}>
                    <div className="tessact-logo"></div>
                    <div className="navbar-item active">
                        <a className="navbar-item-link"><span className="fa fa-comment"></span> TRIVIA</a>
                    </div>

But className navbarSide didn't get applied to div. I see no className for that particular div. What am I doing wrong? Ideally that div should have got className navbarSide.

Anand G
  • 3,130
  • 1
  • 22
  • 28
EdG
  • 2,243
  • 6
  • 48
  • 103
  • I have the exact same in my current config and it works. Can you try to remove the post-css loader and see if it works first ? And try to import your css file with `require`: `const styles = require('./sidebar.css');` – klugjo Jan 23 '18 at 07:07
  • Thanks. This worked. But can I know the rerason? – EdG Jan 23 '18 at 07:38
  • I had the same problem and this made it work, i am also very curious about the reason. – Lukas Bach Jun 04 '18 at 18:39

1 Answers1

0

just add import { StyleSheet } from 'react-native';

import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import styles from './sidebar.css';

class MenuPanel extends Component {
    render() {
        return (
            <div>
                <div className={styles.navbarSide}>
                    <div className="tessact-logo"></div>
                    <div className="navbar-item active">
                        <a className="navbar-item-link"><span className="fa fa-comment"></span> TRIVIA</a>
                    </div>
Riddhi
  • 755
  • 11
  • 31