I have been following a tutorial on React Complete Guide on Udemy, but seems like it is a bit outdated, because after ejecting files, I don't see the same code. I think it is updated today, but as a complete beginner, I have no idea how to continue my course, since I do not know how to import classes which will have unique ID's or how to enable the CSS modules to work...Thank you for your help in advance.
What he sees : Starting from line 162 to 169 This is his code
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
},
},
And what I see : Starting from line 34 to 41 This is my code
// common function to get style loaders const getStyleLoaders =
(cssOptions, preProcessor) => { const loaders = [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: cssOptions,
},
And what I also see is that there are new variables for /\.css$/;
:
Line 28 to 32
// style files regexes
const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
In a React Function Component, we'll use CSS Modules. The code below increases the count value by one and makes use of useState in the FunctionCounter. js Component that will be created. import React, { useState } from "react"; import classes from "./Styles.
If you use CSS Modules to style your React app, you can switch to global scope for the current selector by passing it to :global . Copied! If the selector is switched to global mode, it won't be modified by CSS modules and will be available in the global scope.
CSS Modules let you write styles in CSS files but consume them as JavaScript objects for additional processing and safety. CSS Modules are very popular because they automatically make class and animation names unique so you don't have to worry about selector name collisions.
The code
options: {
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}
shown above as tarting from line 162 to 169 can be added to the other field of webpack.config.dev.js
and webpack.config.prod.js
Instead of test: /\.css$/
,
you will find cssRegex and you can add the code in that section
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true,
localIdentIName: '[name]__[local]__[hash:base64:5]'
}),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With