Building a react 16.4.0 app with bootstrap 4.1.1.
I'm using css-loader with webpack to create custom stylesheets for each component in my app.
My custom styles don't do anything. I thought maybe it was a matter of the bootstrap styles having more points, but even when I add an element that is outside of any bootstrap element, my styles don't take any effect.
Basic example:
Sidebar.js component:
import React from 'react';
import classes from './Sidebar.css';
const sidebar = (props) => {
console.log('classes.red:', classes.red);
return (
<div>
<h3 className={classes.other}>Testing Stylesheet</h3>
<nav className="col-sm-3 col-md-2 hidded-xs-down sidebar" id={classes.red}>
<ul className="nav flex-column">
<li className="nav-item">
<a className="nav-link active" href="#">
FF1493
</a>
</li>
<li className="nav-item">
<a className="nav-link active" href="#">
FF4500
</a>
</li>
</ul>
</nav>
</div>
)
}
export default sidebar;
Here's the super-simple stylesheet:
#red {
background-color: rgba(214, 21, 21, 0.842)
}
.other {
color: green;
}
In the Sidebar.js you can see where I'm console.logging the imported 'red' style. Here's what logs in the browser's console: classes.red: Sidebar__red__2-sUP
<-- It's bringing in the unique identifier as it should.
For those who'll wanna know, here's my webpack.config:
const webpack = require('webpack');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const autoprefixer = require('autoprefixer');
const SRC_DIR = path.join(__dirname, '/client/src');
const DIST_DIR = path.join(__dirname, '/client/dist');
module.exports = {
entry: `${SRC_DIR}/index.jsx`,
output: {
path: path.resolve(__dirname, 'client/dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.jsx?/,
include: SRC_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015'],
plugins: ['syntax-dynamic-import'],
},
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract(
Object.assign(
{
fallback: {
loader: require.resolve('style-loader'),
options: {
hmr: false,
},
},
use: [
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
minimize: true,
// sourceMap: shouldUseSourceMap,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
},
},
{
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',
}),
],
},
},
],
},
// extractTextPluginOptions
)
)
},
],
},
resolve: {
extensions: ['.js', '.jsx']
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development'),
},
}),
new ExtractTextPlugin("styles.css"),
// ,
// new UglifyJSPlugin(),
],
};
Could it be due to the fact that I commented out some things in webpack.config: sourceMap: shouldUseSourceMap,
under options and extractTextPluginOptions
?
I commented them out bc I was getting errors during setup and was not 100% sure their role. I'm still growing in my use of webpack and borrowed some config settings from another app I built using Facebook's create-react-app.
Input is welcome. Thank you in advance! Mike
You should be able to :
import './Sidebar.css'; //don't name the import
then use the class on your element like
<h3 className="other">Testing Stylesheet</h3>
(and as @Leo suggested, open Dev Tools and search for the css to see if it is sent to the browser)
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.