简体   繁体   中英

css modules not working even after ejecting create-react-app

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM