2

I'm trying to define a 's backgroundImage in a map() function but I can't understand what the correct syntax is to add one:

I have a variable named value that stores a file's name and I want to insert uploads/ in my backgroundImage because it's the folde where I want the backgroundImage to fetch the images.

I've tried:

<div className="image-show" key={index} style={{backgroundImage: URL({'uploads/' + value})}}>

But it returns the following error:

Error

Thanks in advance

4 Answers 4

2

You can try out :

style={{backgroundImage: "url(uploads/" + value + ")" }}

//OR

style={{backgroundImage: `url(uploads/${value})` }}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, tried the second option and it worked fine!
@AntónioLima,Glad to know, it helped :)
0

Replace: {'uploads/' + value}

with the following:

{ `uploads/${value}`}

Comments

0

Try this:

<div className="image-show" key={index} style={{backgroundImage: "url(uploads/" + value + ")" }}>

The general syntax is :

backgroundImage: "url(" + background_image + ")"   

--- where background_image is a string(path of  your image)

Comments

0

CSS style fields that you pass in components must have value in number or string.

Instead of style={ {backgroundImage: URL({uploads/12345})} }

React expects style={{backgroundImage: "url(uploads/12345)" }}

I hope that helps

Comments

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.