Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is react removing my class names?

I'm learning Reactjs, and I am rendering a simple page with some components. One of this components is this:

class Header extends React.Component {
    render(){
        return  (
            <header>
                <div class="container">
                    <Logo />
                    <Navigation />
                </div>
            </header>
        );
    }
}

export default Header

I'm using Bootstrap CSS I want the div inside the header to use the styles of container, how ever, after build, the class is gone.

Is there a way to force the attribute class in the components?

like image 695
Pablo Avatar asked Jan 30 '16 15:01

Pablo


People also ask

Is React getting rid of classes?

Yes, React class components will fade away in the future. If you want to embrace modern React, then you should use function components with hooks. That's why you will find most tutorials out there teaching modern React and no class components anymore.

Why does React uses className instead of class?

Explanation: The only reason behind the fact that it uses className over class is that the class is a reserved keyword in JavaScript and since we use JSX in React which itself is the extension of JavaScript, so we have to use className instead of class attribute.

Why is React gaining popularity?

React Builds Robust & Intuitive User Interfaces It allows developers to declaratively describe user interfaces, taking a lot of the heavy lifting out of the coding process while making the code more simple to read and understand.

What is displayName in React?

displayName. The displayName string is used in debugging messages. Usually, you don't need to set it explicitly because it's inferred from the name of the function or class that defines the component.


1 Answers

You have to use the className attribute instead of the class attribute, e.g :

class Header extends React.Component {
    render() {
        return  (
            <header>
                <div className="container">
                    <Logo />
                    <Navigation />
                </div>
            </header>
        );
    }
}

Check the list of Supported Attributes in the documentation.

All attributes are camel-cased and the attributes class and for are className and htmlFor, respectively, to match the DOM API specification.

like image 146
Zakaria Acharki Avatar answered Sep 25 '22 22:09

Zakaria Acharki