114

I'm a bit new to React and trying to set a className as string + prop. But I'm not sure how to do it and whether that is the best practice.

So what I'm trying to do, and it's obviously not working, is this:

<a data-featherlight='string' + {this.props.data.imageUrl}>

How would I go about writing this?

3 Answers 3

248

You can use string concatenation

<a data-featherlight={'string' + this.props.data.imageUrl}>

or use Template strings new ES2015 feature

<a data-featherlight={ `string${this.props.data.imageUrl}` }>
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to concatenate fontawesome string like faTwitter from fa and Twitter for icon={faTwitter}. However this does not accept string, 'casue it's IconProp type. How can we overcome this?
6

You can also try it:

<a data-featherlight={'string1' + this.props.data.imageUrl + 'string2'}>

or

<a data-featherlight={ `string1 ${this.props.data.imageUrl} string2` }>

If you are using Link component then you can concatenation like this:

<Link to={`getting-started/${this.props.message}`}>
     <h1>Under {this.props.message}/-</h1>
</Link>

Comments

3

As far as I know, first:

<a data-featherlight={'your string' + this.props.data.imageUrl}>

Second:

<a data-featherlight={`your string ${this.props.data.imageUrl}`}>

1 Comment

How is this different from the accepted answer from 2016?

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.