Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React className naming convention

As we know, we are supposed to use lowercase and dash for css class name in raw html (e.g. <div class="lower-case-dash" />). What about in React JSX?

For html elements and other React components, what is the naming convention for css class name? camelcase or dash?

<div className="divClass">Something</div>
<div className="DivClass">Something</div>
<div className="div-class">Something</div>
<SomeComponent className="SomeComponent" />
<SomeComponent className="some-component" />
like image 346
MoYummy Avatar asked Jul 26 '19 14:07

MoYummy


People also ask

How do you name a className in React?

Class Name Class names can be anything camelCase , PascalCase , lowercase , can also contain number and special caracters , because after all it is a string. For example, className="myClass MyClass My_Class my-class" etc.

What is naming convention in React?

In React applications, there are 3 most common naming conventions: Camel case for file names, and pascal case for component names. Kebab case for file names, and pascal case for component names. Pascal case for both file names, and component names.

What is the naming convention of a CSS class?

They may be seen as child components, i.e. children of the overall parent component. Using the BEM naming convention, element class names are derived by adding two underscores, followed by the element name.

Should React component files be capitalized?

TitleCase for component files is best because it allows you to know other camelCase files are exporting something else than a component. React is not opinionated on this.


3 Answers

TLDR: PascalCase and Block__Element--Modifier

Check out the official doc of create-react-app. It provides a minimum example of creating a custom component. The js and css filenames as well as the className are all following PascalCase.

// Button.css
.Button {
  padding: 20px;
}

// Button.js
import React, { Component } from 'react';
import './Button.css'; // Tell Webpack that Button.js uses these styles

class Button extends Component {
  render() {
    // You can use them as regular CSS styles
    return <div className="Button" />;
  }
}

Besides, the doc also provides an external link, which describes BEM naming conventions (link) for elements inside the component.

// MyComponent.js
require('./MyComponent.less');
import { Component } from 'react';
export default class MyComponent extends Component {
  render() {
    return (
      <div className="MyComponent">
        <div className="MyComponent__Icon">Icon</div>
        ...
      </div>
    );
  }
}

// MyComponent.less
.MyComponent__Icon {
  background-image: url('icon.svg');
  background-position: 0 50%;
  background-size: fit;
  height: 50px;
}
like image 64
MoYummy Avatar answered Sep 20 '22 13:09

MoYummy


Some of the naming conventions (Recommended) are:

  1. Component Name

    Component name should be in PascalCase.

    For example, MyComponent, MyChildComponent etc.


  1. Attributes

    Attribute name's should be camelCase.

    For example, className, onClick etc.


  1. Inline styles

    Inline styles should be camelCase.

    For example, <div style={{color:'blue',backgroundColor:'black',border: '1px solid',borderRadius:'4px'}}>My Text</div> etc.


  1. Variable Names

    Variable names can be camelCase (Good practice), PascalCase (Avoidable), lowercase, can also contain number and special caracters.

    For example, state = {variable:true, Variable:true, variableName:true} etc.


  1. Class Name

    Class names can be anything camelCase, PascalCase, lowercase, can also contain number and special caracters, because after all it is a string.

    For example, className="myClass MyClass My_Class my-class" etc.

like image 21
ravibagul91 Avatar answered Sep 19 '22 13:09

ravibagul91


That totally depends on your (and your team's) preference. React (nor plain HTML) doesn't restrict you from using lower, dashed or camel-cased class names.

However, I would recommend that you choose an existing CSS convention like BEM. This will make sure that class names stay consistent throughout the process (if followed correctly).

We've chosen for a custom convention in our projects to match our components class names with the component name.

Example:

const NavBar = () => (
  <header className="NavBar NavBar--fixed">
    <div className="NavBar-brand"></div>
  </header>
);

As you can see, this looks a lot like BEM, except for the pascal-cased block, single dash separator for elements and a double dash separator for block modifiers.

like image 37
Chris Avatar answered Sep 18 '22 13:09

Chris