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?
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.
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.
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.
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.
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
andfor
areclassName
andhtmlFor
, respectively, to match the DOM API specification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With