最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Mix ClassName class and props - Stack Overflow

programmeradmin5浏览0评论

I have a React button ponent to which I'm adding props like color, text etc.

At the same time I'm also using Bootstrap css.

So I have a button:

<Button text={sometext} color={success} />

Then what I want to do is this in the ponent:

<div className="btn btn-lg btn-{this.props.color}" role="button">{this.props.text}</div>

See the className and {this.props.color} ... this doesn't work.

How can I do this ... add props inside className?

I have a React button ponent to which I'm adding props like color, text etc.

At the same time I'm also using Bootstrap css.

So I have a button:

<Button text={sometext} color={success} />

Then what I want to do is this in the ponent:

<div className="btn btn-lg btn-{this.props.color}" role="button">{this.props.text}</div>

See the className and {this.props.color} ... this doesn't work.

How can I do this ... add props inside className?

Share Improve this question asked Apr 21, 2017 at 21:57 user7801216user7801216
Add a ment  | 

3 Answers 3

Reset to default 5

You can use template literals to interpolate variables:

<div className={ `btn btn-lg btn-${this.props.color}` } ...

Before rendering the view set a class variable

const classColor = "btn btn-lg btn-" + this.props.color;

Then render:

<div className={classColor} role="button">{this.props.text}</div>

Alternatively, use join:

<div className={['btn btn-lg btn-', this.props.color].join('')}>...
发布评论

评论列表(0)

  1. 暂无评论