Here is how to enable and use CSS modules in React JS.
Step 1: Init git
git initStep 2: Add to git
git add .Step 3: Commit to git
git commit -m "first commit"Step 4: Eject
npm run ejectStep 5: Make changes to config > webpack.config.js
Add the two lines commented as ADDED
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use MiniCSSExtractPlugin to extract that CSS
// to a file, but in development "style" loader enables hot editing
// of CSS.
// By default we support CSS Modules with the extension .module.css
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true, // ADDED
localIdentName: '[name]__[local]__[hash:base64:5]', // ADDED
sourceMap: isEnvProduction && shouldUseSourceMap,
}),
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true,
}
Step 6: In App.js include css as follows
import classes from './App.css';
Step 7: Access classes in css file as follows:
<div className={classes.App}>
Comments
Post a Comment