It looks like you're almost there, but you're not properly concatenating the url
variable within the style
object. When you use curly braces {}
within JSX, you're indicating that you want to embed JavaScript expressions within the JSX code. In your case, you need to ensure that the url
variable is properly evaluated and inserted into the backgroundImage
property.
Here's the correct way to do it:
var url = "/lectures/" + id + "/thumbnail.jpg";
style={{
backgroundImage: `url(${url})`,
}}
In the code above, I've used template literals (denoted by backticks) to embed the url
variable within the backgroundImage
property. This way, the actual value of url
will be inserted into the URL string, and it will correctly display the different images depending on the id
.
So, for example, if id
is 1, the generated style will be:
style={{
backgroundImage: "url(/lectures/1/thumbnail.jpg)",
}}
And if id
is 2, it will be:
style={{
background-image: "url(/lectures/2/thumbnail.jpg)",
}}
And so on, adapting to the different values of id
.