Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add multiple className's to react component?

Tags:

reactjs

I need to add one className to a component which is being passed down from the parent, and another className that doesn't have anything to do with its parent, but with the component itself.

example

<div className={this.state.className} className={this.props.content.divClassName}>       <div className={this.props.content.containerClassName}>       <div className="row">       <div className="col-sm-6 col-sm-offset-6">         <p>{this.props.content.subTitle}</p>         <h1 className={this.props.content.titleClassName}>{this.props.content.headerText}</h1>         <p className={this.props.content.subTitleClassName}>{this.props.content.paragraph}</p>         <p>{this.props.content.quote}</p>         <Button showMe={this.props.content.showButton} buttonText={this.props.content.buttonText} />       </div>       </div>       </div>     </div> 

When I try this, neither class is applied, I can do one or the other or not both. How do I achieve this?

like image 342
ceckenrode Avatar asked Apr 21 '16 14:04

ceckenrode


People also ask

Can a div have multiple classNames?

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well.

How do I add multiple classNames in material UI?

To add multiple classes in React Material UI using the classes props, we can use the classnames package. We call makeStyles with an object with the class names as the property names. And we set each property to an object with the CSS styles as the value. Next, we call useStyles to return the classes object.

How can I add two classNames in HTML?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.


2 Answers

<div className={`${this.state.className} ${this.props.content.divClassName}`}> 

Or if you can't use template strings:

<div className={[this.state.className, this.props.content.divClassName].join(" ")}> 
like image 106
JMM Avatar answered Oct 27 '22 01:10

JMM


Using template literals (Template strings),

Combo of string and props class is here

className={`${props.myClass} MyStringClass`} 
like image 42
Muhammad Muzamil Avatar answered Oct 27 '22 00:10

Muhammad Muzamil