In my React project (made using create-react-app), I have an array of objects with the following format:
items : [{
id:"test",
title:"Test title",
project_url:"/project/test",
doc:"2018",
description:"test description",
link:"example.com",
background_img:"../../../s.png",
color:"#333333"
}]
I map these objects into elements to be displayed on the page:
_generateItems() {
return this.state.items.map((item) => {
return (
<div className="item">
<h3>{item.title}</h3>
<div className="device" style={{backgroundImage:`url(${item.background_img})`}}/>
<Link to={item.project_url}><div className="info_btn"><h6>More info</h6></div></Link>
</div>
)
})
}
My problem is that I cannot get background image of the inner div to work. After scouring the internet, I tried to use template literals like so:
`url(${item.background_img})`
This allowed the project to compile and run without errors, and inspecting the div in the browser, I can see that it has the correct style. The image however does not appear on the div.
I am guessing it has to do with the way webpack handles static resources like images when it builds the project. How can I make it work?
../../../filename(which is relative) is not relative to the page you are on nor the css url. Can you switch the urls to absolute urls?/path/to/file(be aware of the leading/) ?