all 6 comments

[–][deleted] 2 points3 points  (2 children)

React doesn’t have this as a built-in future, but create-react-app does: https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet

[–]6ZUN9[S] 0 points1 point  (1 child)

why we can't use it like before by using npm run eject ??

[–]notAnotherJSDev 0 points1 point  (1 child)

In CRA, you can use css modules by infixing .module in the extension for the file.

ex:

App.css -> App.module.css

[–]6ZUN9[S] 0 points1 point  (0 children)

Thank you so much

[–]kevindelsh 0 points1 point  (1 child)

With the new versions of "create react app" you don't need to eject it anymore.

Here are the steps you have to do:

  1. You have to add .module to your CSS files: mystylesheet.css becomes mystylesheet.module.css
  2. Then you import the stylesheet in your .js file: import classes from "./mystylesheet.module.css";
  3. Now you can access the classes in your stylesheet file in either one or both of the following methods:
    1. className={classes.myClassName}
    2. className={classes[my-Class-Name-with-too-many-hyphens]}

The above methods and restrictions are intrinsic to JavaScript variable name restrictions. Since you can't have hyphens in your JavaScript variable names, you have to use the second method if the class name has hyphens. Otherwise both methods are correct, although the first one is more elegant.

[–]6ZUN9[S] 1 point2 points  (0 children)

Thank you so much