title: How to iterate through a directory of images using React
body: <p>Here's my situation:</p>
  
  <p>I have a directory of folders that contain image files inside them. For example: images/(ID)/(ID)-1 (or 2, 3, etc.).jpg. Where (ID) is variable (I have hundreds of IDs within the images directory).</p>
  
  <p>I'm new to React and still trying to get my way through how to use loops in it. I've tried several different approaches and none have seemed to worked on this specific case.</p>
  
  <p>How would I go about constructing a loop that will iterate over each folder, dynamically, and render the images inside each folder? I have a carousel that I want to call this loop in.</p>
  
  <p>Thank you for any help.</p>
  
  <p>EDIT: I've tried working with these two solutions:</p>
  
  <p><a href="https://stackoverflow.com/questions/45382713/how-to-iterate-images-on-react">How to iterate images on React?</a></p>
  
  <p>With my own context:</p>
  
  <pre><code>const images = [{ src: "./images" + ID, alt: "Your description here 1" }];
  
  // ...
  
  {
    images.map(function(imageProps) {
      return (
        &lt;li key={imageProps.src}&gt;
          &lt;img src={imageProps.src} alt={imageProps.alt} /&gt;
        &lt;/li&gt;
      );
    });
  }
  </code></pre>
  
  <p>And</p>
  
  <p><a href="https://stackoverflow.com/questions/42118296/dynamically-import-images-from-a-directory-using-webpack">Dynamically import images from a directory using webpack</a></p>
  
  <p>With my own context:</p>
  
  <pre><code>function importAll(r) {
    return r.keys().map(r);
  }
  
  const images = importAll(
    require.context("./images" + ID, false, /\.(png|jpe?g|svg)$/)
  );
  </code></pre>
  
  <p>With the latter, I figured out that within <code>require.context</code>, you can't have a dynamic variable within the argument otherwise it just throws you an error. The first example, it told me that .map wasn't a function. The error wasn't much help.</p>