Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of using bsClass and className with react-bootstrap?

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.

like image 205
Zuzu Softman Avatar asked Apr 18 '17 02:04

Zuzu Softman


People also ask

What is the difference between class and className in React?

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.

Should I use Reactstrap or React-Bootstrap?

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.

What is the difference between Bootstrap 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.

What is the use of className in React?

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.


2 Answers

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.

like image 77
Shubham Khatri Avatar answered Oct 19 '22 19:10

Shubham Khatri


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.

like image 1
Denny Avatar answered Oct 19 '22 20:10

Denny