37

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?

6 Answers 6

89
<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(" ")}>
Sign up to request clarification or add additional context in comments.

2 Comments

Is this preferred over simple concatenation? {this.state.className + " " + this.props.content.divClassName}
@James Depends who you ask. Personally I'd probably just do the [].join() way that I just added, but I'd probably prefer the template string version to concatenation.
10

Using template literals (Template strings),

Combo of string and props class is here

className={`${props.myClass} MyStringClass`}

Comments

0

I use classnames like this:

# terminal - install
$ yarn add classnames

// React component
import classnames from 'classnames';

<Component classNames={classnames(class1, class2)} />

Comments

0

Best way at the moment if you using css modules is as array. And there you can add regular classes without modules. Here is example:

<div className={[styles.restaurantEntry, styles.barEntry, 'alice', 'text-center']}</div>

1 Comment

it does not work for me: it say "TS2322: Type 'string[]' is not assignable to type 'string'."
-1

<div className={this.state.className && this.props.content.divClassName></div>

4 Comments

Would be nice, if you can write why and how.
This would set className to a boolean. (It's also a syntax error because it's missing the }, but would not have the desired effect despite that.)
Indeed is missing the } , my bad. But, in my apps I can work with this kind of method just fine. I also use ||, and it still works.
My mistake saying it would set className to a boolean :facepalm:, but it is applying boolean logic, so it's not concatenating the values or resulting in both being applied: it's applying one or the other. If both are non-empty strings and you && them, you'll get only the second one applied. If you || them you'll get only the first one applied. Whereas the question is asking how to apply both.
-2
<div className={(classes.shiny, classes.round)}>
    i'm shiny and round.
</div>

This works with React + Material-UI makeStyles.

3 Comments

I do honestly want anyone with information to comment on why this is not a good solution, as well as those who don't know, to know that it doesn't conform to any best practices. Any suggestions as to how I could convey that message are greatly appreciated.
Please put those parts which are not answering into a comment.
Sorry, but this is an invalid answer. Only second class is ever used. This is roughly translated to {className: ("shiny", "round")} which results in {className: "round"} MDN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.