2

I am trying to render image from array by placing its name in middle of <img> link.

At this time I have ran out of links that could help me...

I tried everything that came to my mind. Everything displays as it should except image...!

this is array:

cars: [
        {
          'carname': '2013 Toyota GT86', 
          'owner': 'Ryan',
          'desc': 'Aero Paket, BC Coilover, 3sdm 0.01 black matte, Invidia N1 Catback, Valenti taillights, camouflage vinyls, Vortech supercharger incl. oil cooler - 325hp daily driver',
          'image': 'toyota'
        }]

Render section:

render() {
    const cars = this.state.cars.map((car, i) => {
      return (
        <div key={i}>
          <div className="col-md-12 cBusiness">
            <h2>
              { car.carname }
            </h2>
              <p>Owner: { car.owner }</p>

              <p>{ car.desc }</p>

              <p>

                <img src={"pages/upload/{car.image}.jpg"} />

              </p>

          </div>
        </div>
      );
    });

structure:

enter image description here

2 Answers 2

2

Do something like this:

 <img src={`pages/upload/${car.image}.jpg`} />
Sign up to request clarification or add additional context in comments.

Comments

1

Try using template literals:

<img src={`pages/upload/${car.image}.jpg`} />

or old skool for all the IE folks out there:

<img src={"pages/upload/" + car.image + ".jpg"} />

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.