I am new to react and could not grasp the concept between bsClass and className.
I try to implement a modified button style, like:
<Button bsClass="btn-custom" >Custom button</Button>
where it does not work when I substitute bsClass
with className
.
But in other part, using the same custom.css source, I implement:
<img src={logo} alt="logo" className="app-logo" />
and it works.
js. export default App; Output: 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.
Reactstrap also has fewer features than React-Bootstrap. However, Reactstrap is easier to learn and use than React-Bootstrap. This article will compare the two most popular front-end frameworks, Reactstrap and React-Bootstrap.
Bootstrap is a CSS framework that improves responsiveness. React is a JavaScript library and is used to improve UI. Bootstrap is based on the CSS grid system, the CSS flexbox, to improve responsiveness. React is based on pure vanillaJS to improve the UI and client-side experience.
In most cases, className should be used to reference classes defined in an external CSS stylesheet. style is most often used in React applications to add dynamically-computed styles at render time.
JSX attribute className
is equivalent of HTML class
. So the below JSX
<span className="app-logo">Logo</span>
will be equivalent to the below in HTML
<span class="app-logo">Logo</span>
As per bsClass is considered in
<Button bsClass="btn-custom" >Custom button</Button>
it is prop
that is being passed on to the Button
component in reactJS and that is what it will be using to set the className
inside the component something like
<button className={this.props.bsClass}>{this.props.children}</button>
So it an attribute that is defined as a property by the react-bootstrap
docs.
React's className
is exactly equivalent to regular classes in CSS.
HTML/CSS:
<div class='red-text'>
Foo
</div>
React/JSX:
<div className='red-text'>
Foo
</div>
The above snippets of code do the exact same thing. The only reason we're stuck with using className
in React (instead of class
) is because "class" was already taken as a reserved keyword in JavaScript.
As the others have said, bsClass
is a pre-defined class within the react-bootstrap package. Just like how the CSS-version of Bootstrap comes with its own styling, so, too, does react-bootstrap.
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