我弹出了我的create-react-app以使用css模块。我从
修改了我的webpack.config.dev.jsconst redbox = document.querySelector("#redbox");
redbox.style.width = `50px`;
const coolbutton = document.querySelector("#coolbutton");
coolbutton.addEventListener("click", animator);
let toggle = true;
function animator() {
let poller = setInterval(function(){
let w = parseInt(redbox.style.width, 10);
toggle ? w++ : w--;
redbox.style.width = w + 'px';
if (w == 100 || w == 50){
toggle = !toggle;
clearInterval(poller);
}
if (w == 80)
log(redbox.style.width);
}, 20);
}
function log(args){
console.log(args);
}
然后我尝试在我的一个组件中使用css模块
{
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',
}),
],
},
},
],
}
但是className navbarSide并没有应用于div。我没有看到特定div的className。我究竟做错了什么?理想情况下,div应该有className navbarSide。
答案 0 :(得分:1)
只需添加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>