I am new to React and I appreciate if anyone could help...
I set up the environment using "create-react-app", and all image files are in img folder under src folder. One of components sets background image dynamically.
This works fine:
<div style={{ background: `url(${require("../img/file-name.jpg")}) no-repeat`}}/>
However, when I tried to use a variable for the file name (something like shown below), I get the error: Error: Cannot find module '../img/file-name.jpg'
let imageUrl = '../img/' + filenames[0];
...
<div style={{ background: `url(${require(imageUrl)}) no-repeat`}}/>
I also tried something like below just for a test, but I got the same error.
let imageUrl = '../img/file-name.jpg';
...
<div style={{ background: `url(${require(imageUrl)}) no-repeat`}}/>
I appreciate if anyone could help!! Thank you!