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?
To pass class names as props to a React component:Pass a string containing the class names as a prop. Destructure the prop in the child component. Assign the class names to an element, e.g. <h2 className={className}>Content</h2> .
You want to use JSX inside your props You can simply use {} to cause JSX to parse the parameter. The only limitation is the same as for every JSX element: It must return only one root element.
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}` }>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With