How can I dynamically import a large list of images from a folder in a Create React App
?
- src
-- images
--- image1.png
--- ...
--- image50.png;
const imageUrls = ['image1.png',..., 'image50.png'];
React.useEffect(() => {
const getFeaturedUsers = async () => {
try {
const data = Promise.all(
imageUrls.map((url) => import(`../images/${url}`))
);
setData(data);
} catch (error) {
console.log(error);
}
};
getFeaturedUsers();
}, []);
The above 'solution' throws the error
Uncaught (in promise) Error: Cannot find module './'