question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the best practice of using css in CRA ?

See original GitHub issue

Generally, we recommend that you don’t reuse the same CSS classes across different components. For example, instead of using a .Button CSS class in <AcceptButton> and <RejectButton> components, we recommend creating a <Button> component with its own .Button styles, that both <AcceptButton> and <RejectButton> can render . Following this rule often makes CSS preprocessors less useful, as features like mixins and nesting are replaced by component composition.

This paragraph comes from the section Adding a CSS Preprocessor (Sass, Less etc.). I am confused about how to avoid nesting of classname. For example, I have a CustomComponent:

class CustomComponent extends React.Component {

  render() {
    <div className="customComponent">
        <h1 className="title">This is title</h1>
        // ... other elements
    </div>
  }
}

The css file for CustomComponent:

.customComponent{ }

.customComponent .title { }

In order to avoid conflict of class name,I still need to nest class names like .customComponent .title . Could anyone give me an example that using component composition to replace nesting?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
xuchaobeicommented, Sep 27, 2017

@miraage @Timer I know of BEM and styled-component. I am just trying to understand what the paragraph means. If I use Less, I can define the css styles in a easy nesting way:

.customComponent {
  .title {
   }
 }

But the paragraph said Following this rule often makes CSS preprocessors less useful, as features like mixins and nesting are replaced by component composition. I don’t see how the component composition can replace nesting feature of CSS preprocessors . I hope there is a more detailed example.

1reaction
adambowlescommented, Sep 29, 2017

CSS Modules will hopefully be coming in the future: #2285

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Style Your React App – 5 Ways to Write CSS in 2021
CSS written in a stylesheet is a great first choice for your application. Unlike inline styles, it can style your application in virtually...
Read more >
React-CSS best practices | Best way of using CSS with ReactJs
In this video we have discussed about how should you be using CSS in your React Project to avoid chaos Tryout the demo...
Read more >
10 React CSS Best Practices - CLIMB
In this article, we'll be discussing 10 React CSS best practices that will help you write cleaner and more maintainable code.
Read more >
React Guide: Best Practices for Styling Components - Pluralsight
Check out the various styling options available along with their related best practices.
Read more >
How To Style And Write CSS In React | LambdaTest
In short, inline CSS is best suited for learners but not for implementing real-world web projects. Here is why you should not use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found