Access props inside quotes in React JSX
Asked Answered
D

10

328

In JSX, how do you reference a value from props from inside a quoted attribute value?

For example:

<img className="image" src="images/{this.props.image}" />

The resulting HTML output is:

<img class="image" src="images/{this.props.image}">
Dionne answered 10/2, 2014 at 2:16 Comment(0)
B
512

React (or JSX) doesn't support variable interpolation inside an attribute value, but you can put any JS expression inside curly braces as the entire attribute value, so this works:

<img className="image" src={"images/" + this.props.image} />
Brine answered 10/2, 2014 at 2:21 Comment(2)
what about backtips in es6?Tham
@DavidLavieri See Cristi's answer https://mcmap.net/q/98513/-access-props-inside-quotes-in-react-jsx below.Dillard
P
248

If you want to use the es6 template literals, you need braces around the tick marks as well:

<img className="image" src={`images/${this.props.image}`} />
Poisoning answered 5/5, 2015 at 19:11 Comment(2)
future ES6 Template literalsGammy
The string enclosed by backticks is a "template literal": developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Legitimacy
J
51

If you're using JSX with Harmony, you could do this:

<img className="image" src={`images/${this.props.image}`} />

Here you are writing the value of src as an expression.

Justajustemilieu answered 4/3, 2015 at 18:27 Comment(2)
this is something that's hard to find. and for reusable containers this is a-must-knowWellgrounded
Just so that people don't get confused with mentioning of Harmony, this is a part of ES6 standard, while answer is dated few months before it became a standard.Tongue
S
18

Instead of adding variables and strings, you can use the ES6 template strings! Here is an example:

<img className="image" src={`images/${this.props.image}`} />

As for all other JavaScript components inside JSX, use template strings inside of curly braces. To "inject" a variable use a dollar sign followed by curly braces containing the variable you would like to inject. For example:

{`string ${variable} another string`}
Samanthasamanthia answered 30/6, 2018 at 21:44 Comment(0)
F
8

Best practices are to add getter method for that :

getImageURI() {
  return "images/" + this.props.image;
}

<img className="image" src={this.getImageURI()} />

Then , if you have more logic later on, you can maintain the code smoothly.

Fourscore answered 20/2, 2017 at 19:58 Comment(0)
P
2

For People, looking for answers w.r.t to 'map' function and dynamic data, here is a working example.

<img src={"http://examole.com/randomview/images" + each_actor['logo']} />

This gives the URL as "http://examole.com/randomview/images/2/dp_pics/182328.jpg" (random example)

Plagiary answered 18/11, 2015 at 22:35 Comment(0)
E
2

You can do like this

<img className="img" src=`images/${this.props.image}`>
Esquiline answered 23/6, 2022 at 9:58 Comment(0)
T
1

Note: In react you can put javascript expression inside curly bracket. We can use this property in this example.
Note: give one look to below example:

class LoginForm extends React.Component {

  constructor(props) {
    super(props);

    this.state = {i:1};
  }

  handleClick() {
    this.setState(prevState => ({i : prevState.i + 1}));

    console.log(this.state.j);  
  }


  render() {

    return (
      <div>
        <p onClick={this.handleClick.bind(this)}>Click to change image</p>
        <img src={'images/back'+ this.state.i+'.jpg'}/>
      </div>
      );

  }
}
Thresathresh answered 24/1, 2018 at 12:41 Comment(0)
B
1

Here is the Best Option for Dynamic className or Props , just do some concatenation like we do in Javascript.

     className={
        "badge " +
        (this.props.value ? "badge-primary " : "badge-danger ") +
        " m-4"
      }
Backwash answered 6/8, 2018 at 7:25 Comment(1)
I'd recommend uses classnames package for building dynamic classNamesTham
A
1

you can use

<img className="image" src=`images/${this.props.image}`>

or

<img className="image" src={'images/'+this.props.image}>

or

  render() {
         let imageUrl = this.props.image ? "images/"+this.props.image : 'some placeholder url image';
    return (
       <div>
          <img className="image" src={imageUrl} />
       </div>
    )
  }
Artema answered 15/1, 2020 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.