0

I am using a function which will return the full path of the image.

  const imagePath = (imagePath) => {
    const domainPath = "http://localhost:9005/gallery/original/";
    const fullImagePath = domainPath + imagePath;
    return fullImagePath;
  };

Now If I use below code, it will work and show image on browser:

<img
 src={imagePath(item.mediaPath)}
 alt="cow images"
/>

But If I use below code, it will NOT work:

<div
  className="bg-image-blur"
  style={{
    backgroundImage:
      "url(" +
      imagePath(item.mediaPath) +
      ")",
  }}
></div>

Please see given below image from my inspect element. Style is added to the element :
enter image description here

and It will also not work if I don't use imagePath function :

<div
  className="bg-image-blur"
  style={{
    backgroundImage:
      "url(" +
      "http://localhost:9005/gallery/original/" +
      id.mediaPath +
      ")",
  }}
></div>

What is the issue ?

1 Answer 1

2

It does not work because you haven't put '' for url. And I'd suggest you use template literals for your string concat instead for easier look

<div
  className="bg-image-blur"
  style={{
    backgroundImage: `url('${imagePath(item.mediaPath)}')`,
  }}
></div>

You can check this document to understand how background-image looks like with url

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I am already using template string. I just wrote an example here without template string

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.