1

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 './'
fitzmode
  • 1,007
  • 1
  • 18
  • 29
  • https://stackoverflow.com/questions/53775936/import-image-dynamically-in-react-component this answer might help. – vovchisko May 23 '21 at 17:32

0 Answers0