Create-react-app: What is the best practice of using css in CRA ?

Created on 26 Sep 2017  路  5Comments  路  Source: facebook/create-react-app

Generally, we recommend that you don鈥檛 reuse the same CSS classes across different components. For example, instead of using a .Button CSS class in and components, we recommend creating a

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锛孖 still need to nest class names like .customComponent .title . Could anyone give me an example that using component composition to replace nesting?

question

Most helpful comment

@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.

All 5 comments

If you ask me I'd name them like CustomComponent and CustomComponent__Title (e.g. similar to BEM way).

On the other hand, if you have such kind of title to be very similar across the pages/components - extract it to the separate component.

// EDIT

BTW, your question is out of scope of CRA itself.

The solution here would be to use more descriptive class names 馃槃.

You could try out a CSS solution, there's a number which don't require you to eject like styled-components.

@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.

if you aren't use pre-processors, using camel case and chaining class names together can become sloppy. its best to use lower case and hyphenate. Uppercase the first letter of styles that cascade with inheritance.

.btn-small-search-title Dark Bold where you have the first part be the xpath of the component, and Dark and Bold be generic style modifiers that should work for any type of component

This is called Modular CSS Naming here is an article explaining it. This is utilized by GWT, and can be found on any google webpage. This convention becomes more apparent when you start dealing with complex UI component layouts. GLHF

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Aranir picture Aranir  路  3Comments

JimmyLv picture JimmyLv  路  3Comments

ap13p picture ap13p  路  3Comments

alleroux picture alleroux  路  3Comments

dualcnhq picture dualcnhq  路  3Comments