1

How can I use my own class names when using CSS and Style loaders? I'm using React and i often get confused with all the class names and the related components. If it is not recommended to keep the class names, how can I organize my code in a manner that it don't get messy?

1 Answer 1

1

You can use normal classNames and then import the relative .css file that has the style for those classes. Something like this:

MyComponent.js

...
import '/path/to/css/style.css'
...
render() {
  return(<div className="MyComponent">Hello</div>);
}
...

style.css

...
.MyComponent {
  width: 100px;
  background-color: red;
  ...
}
...

And this would work just fine, with this approach though you are in charge of naming your classes and making sure there are no collisions and everything is named properly (you can then follow BEM techinque or others as you prefer).

Otherwise there are other approaches that I am not gonna explain into details because the relative docs are great and don't need any addition.

You can use Css Modules or Styled Components. There is also this article that has a good overview of these methods and can help you out make a final decision.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.