2

So in php if we want to refer to a source of an image, we can do it like this

<img src="images/<?php $imageName; ">" />

But in JSX of react, we can only do like

<img src={image.path} />

But since I have only stored the name of the image, I need to do something like this

<img src="images/{image.name}" />

I do know that image.name is a string rather than a variable as in PHP. But is there a way to make this write?

NOTE: I cannot create a constant string too since that should be written in this.something.map(smth => "I am wriitng it here")

Kyi Zin
  • 823
  • 1
  • 6
  • 15
  • What you need is a template string, you can check the accepted answer to this post as an example, or just google "javascript template string": https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript – Jayce444 Feb 26 '20 at 07:48
  • Thank you, I didn't remember string literals from python – Kyi Zin Feb 26 '20 at 07:59

1 Answers1

0

You can try:

<img src={`images/${image.name}`} />
Ryan Nghiem
  • 2,417
  • 2
  • 18
  • 28