score:120
You do not need to eject.
Create React App supports CSS Modules right out of the box as of version 2.
Upgrade to the latest (react-scripts@latest
) version.
If you use yarn:
yarn upgrade react-scripts@latest
If you use npm:
npm install --save react-scripts@latest
Then you just have to create a file with the extension .module.css
For example:
.myStyle {
color: #fff
}
Then you can use it like so:
import React from 'react'
import styles from './mycssmodule.module.css'
export default () => <div className={styles.myStyle}>We are styled!</div>
score:0
Depending on your react-scripts
version (you can check the version in package.json
file):
For react-scripts
version before 2.0.0
:
if you are using git in your project you should commit your changes and push them.
do
npm run eject
edit your webpack config files
dev
andprod
with these changes:if you are running your app, stop it and rerun it, hence you will find your changes and can then use CSS modules.
Starting with react-scripts
version 2.0.0
:
You can follow
"Adding a CSS Modules Stylesheet" from the official create-react-app
documentation.
score:0
Heading ##i use react-script version 3.4.3 (latest version). you simply follow the below given steps.
run the command :npm run eject
Then you will get a config file , in that open webpack.config.js
And search test: cssRegex or just search cssRegex now you will see something like this:
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
}),
now after importLoaders:1, add modules:true, and just save the file.
Now you can restart the server using npm start and you will see your styles applied.
Open .js file and import statement like this
import cssStyleClassName from './MyAppStyle.css';
please do not forget to add .css extension to the styling file, at the time of import.
Then this import statement allow you to do following changes in component tags like this
<div className={cssStyleClassName.styleName}></div>
styleName
is what you defined in your MyAppStyle.css
file
.styleName{
your properties here...
}
score:0
In webpack.config.js file find this keyword 'css-loader'.
You will find below code in line number 82 for react version-16.13
{
loader: require.resolve('css-loader'),
options: cssOptions,
}
Replace above object with
{
loader: require.resolve('css-loader'),
options: {
modules: {
mode: "local",
localIdentName: "[name]_[local]_[hash:base64:5]"
},
import: true,
importLoaders: true
}
}
Start the server again by npm start(If changes are not reflected)
score:0
There is a fresh library called patch-styles package.
It's introducing a more declarative way of applying styles. You need to wrap your code with <PatchStyles classNames={style}>
where style
is the default import from the style module. See the example of usage in StackBlitz.
score:0
With React-17.0.2, Run below script
npm run eject
Go to file
webpack.config.js
Search for
cssRegex
Add module object as below
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
modules: {
localIdentName: "[name]__[local]___[hash:base64:5]"
}
})
Then your css file will work with name Eg. header.css
score:1
You need to eject create-react-app and then in config files for webpack you add these 2 lines
And to use it load css to Component you wan't (i keed component and it's css in same folder)
import classes from './SomeComponentStyle.css';
...
<div className={classes.RedDiv}>
inside SomeComponentStyle.css
.RedDiv {
/* styles for red div */
}
score:1
You are using version 1.1.4 , so CSS Modules doesn't work by default ... above version 2.0, it works default by changing the css file name to name.module.css.
score:1
In case you ran npm run eject
and you still want to enable it with the 'webpack.config.js' file you can add the modules object to test: cssRegex
section. Like so:
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
modules: {
getLocalIdent: getCSSModuleLocalIdent,
},
}),
score:2
i am using 3.4.1
version of the react-scripts
so here is my approach
- run the command :
npm run eject
2.Then you will get a config file , in that openwebpack.config.js
- And search
test: cssRegex
or just searchcssRegex
- now you will see something like this:
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
}),
now after importLoaders:1,
add modules:true,
and just save
the file.
5.Now you can restart the server using npm start
and you will see your styles applied.
(if you already know them how to use)
here when I used [name]__[local]__[hash:base64:5]
because I got an error that
- options has an unknown property 'localIdentName'. These properties are valid:
object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals?, esModule? }
so I removed it and I just used modules:true
and it worked for me.
score:10
To enable CSS module in to your app, you don't need to eject create-react-app. You can follow these simple steps described in this link to use CSS module in your project.
But in case if you ejected your create-react-app, then you must find file named
"webpack.config.js"
open this file and find this {test:cssRegex....etc} in line no. 391 and replace to this changes:
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]__[hash:base64:5]'
}),
},
Open .js file and import statement like this
import cssStyleClassName from './MyApp.css';
Then this import statement allow you to do following changes in component tags like this
<div className={cssStyleClassName.styleName}></div>
styleName is what you defined in your MyAppStyle.css file
.styleName{
your properties here...
}
After eject your app, you must restart your local server, sometime changes don't get reflect.
Note In earlier version there were two generated file for production and dev separately. Now in current version one file named "webpack.config.js" file for which you need to concerned for changes. Thank you.
Source: stackoverflow.com
Related Query
- how to use .env.qa or .env.staging with create react app
- How to use Turborepo for an existing react app created with Create React App in order to make it a monorepo?
- How to determine the order of css in my create react app build with webpack and customize-cra?
- How to use @apply in a react component with css modules
- How to use css modules (react semantic ui) in create react app?
- How can you use CSS modules together with Typescript, React and Webpack?
- How to Create global styles with Styled components in react and next js and is it better to use combination of css files and styled component?
- How to use nested css class (cascade) with React CSS Modules
- How to use CSS Modules with webpack in React isomorphic app?
- How to apply global styles with CSS modules in a react app?
- How to import CSS modules with Typescript, React and Webpack
- How to use css modules with create-react-app?
- Use CSS Modules in React components with Typescript built by webpack
- How to style child components in React with CSS Modules
- How to use vw and vh css with React Native
- How to use a global parent selector with CSS Modules
- What's the easiest way to use Create React App with Relay?
- How to use yarn to create a React app project?
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- How do you use CSS grid layouts with React effectively?
- How to avoid very long paths and use absolute paths within a Create React App project?
- how to config create react app with worker-loader
- Use PrimeReact Themes with CSS Modules Enabled in React Application
- Can I use Create React App Jest Testing with a test reporter without ejecting?
- How to use Sass and CSS Modules with create-react-app?
- Theming react app with sass and css modules
- How get window values in Tests with Enzyme, Jest + Create React App
- Using LESS CSS with the latest Create React App
- How to use sitemap in firebase hosting with react app
- Is it possible to use MaterialUI with React and css modules and access the theme inside the css module file?
More Query from same tag
- TodoList with ReactJS - Problem to implement the edit content button logic
- How to pass AppSettings from ASP.NET Core MVC Controller to ReactJS / Redux ClientApp?
- How to manage dynamic variables from external config file using Electron and React
- What is the best way to position span element under 2 flexbox row elements
- How to add more action icons to material UI accordion?
- How to define two way for run code in filter method
- Apollo useLazyQuery hook uses old variables when refetching (instead of new vars)
- React Leaflet map to PDF
- Sharing session between React front-end server and PHP/Symfony back-end server
- Using a variable in place of a string
- Can we add properties to immutable object in JavaScript?
- How can I use multiple props in a single callback defined in a template literal in React styled-components
- Typescript set object key by variable on an interface
- Test execution with Jest and Context
- Expected Array but received array in Jest
- How do I pass an element's property to a function in React?
- I wonder if this really is the correct way to use onAuthStateChanged
- TypeError: _firebase_config__WEBPACK_IMPORTED_MODULE_1__.db.collection is not a function firebase
- Context value undefined in React
- Creating package with customized component
- Firebase firstore auto increment number not working (React js)
- unused expression, expected an assignment or function call
- change useState when redux state change
- unable to get context api values
- ReactJS: Calling 'this' within a JQuery event listener function
- populating the neccessary data in react.js
- Material-ui paper doesn't render on screen
- ReactJS : How to detect network idle between XHR requests
- Dynamically create an array of inputs in React
- Reset Redux state to undefined or initial state except one state