I’ve looked everywhere for answers regarding this and found none.
I have a simple functional component in React below.
There are some images in src/images that im trying to import without manually writing a bunch of import statements, one for each picture, like we’d import useEffect etc. Thats because this component will be called for many items so I want a way to dynamically import one of those images
props.item.imageSource is a relative path that has the EXACT same value as the string i hardcoded in the snipped below this one. I’ve checked with React’s debugging tools for the browser. This gives me a "Cannot find module" error. Ive tried without "" + too.
function MenuItemCard(props) {
const [importedImage, setImportedImage] = useState(null);
useEffect(() => {
import("" + props.item.imageSource).then((image) =>
setImportedImage(image.default)
);
}, []);
return (
<div className="menuItemCard">
{importedImage && <img src={importedImage} />}
</div>
);
}
However this works when hardcoding the path
function MenuItemCard(props) {
const [importedImage, setImportedImage] = useState(null);
useEffect(() => {
import("../../images/burgers/burger-1.png").then((image) =>
setImportedImage(image.default)
);
}, []);
return (
<div className="menuItemCard">
{importedImage && <img src={importedImage} />}
</div>
);
}
Ive tried saving the imageSource value in another variable and passing that to import, with/without "" +, none work so I’m guessing its not specifically something to do with my props rather how I’m using the variables. The only thing that works is passing in the hardcoded string directly into import.
Similar issue with using require. This doesnt work
return (
<div className="menuItemCard">
<img src={require(props.item.imageSource)} />
</div>
);
But this does
return (
<div className="menuItemCard">
<img src={require("../../images/burgers/burger-1.png")} />
</div>
);
2
Answers
Is there a reason you do import for images?
If the image url is in the props, you can just do it like this: https://codesandbox.io/s/dynamic-images-dyzsr1
also do take a look at the actual component -> https://codesandbox.io/s/dynamic-images-dyzsr1?file=/src/components/MenuItemCard.js
you don’t even need useEffect or state there
if your data looks something like this:
then your component might look like this:
EDIT:
If you are trying to import hundreds of images, check this answer out: https://stackoverflow.com/a/48121563/3407499
I just updated code sandbox example that I posted above, you can check that setup that will allow you to use the relative paths for images
As was mentioned in the comments, CRA uses webpack under the hood, so
require.context
can be used easily.Assuming you have a directory called
images
in thesrc
directory, you could do the following to import all of the images in the directory and map over them to output them: