3

I have created a project with create-react-app and I have a folder called images inside the source. I want to loop inside the images folder and display them.

So far my code look like this:

import React, { Component } from 'react'
import images from './images/wd/'

const listOfImages = []

class Myloop extends Component {
  importAll(r) {
    return r.keys().map(r)
  }

  componentWillMount() {
    listOfImages = this.importAll(require.context(images, false, /\.(png)$/))
  }

  render() {
    return (
      <>
        <ul>
          <li>
            {listOfImages.map((images, index) => (
              <img src={images} key={index} alt="info"></img>
            ))}
          </li>
        </ul>
      </>
    )
  }
}

export default Myloop

After saving the file I have the following message on the terminal:

Module not found: Can't resolve './images/wd/' in '/mywebsite/src/components'

I'm not sure what is wrong, but any help will be great.

6
  • Have a look at this question: stackoverflow.com/questions/42118296/… Commented Oct 12, 2020 at 20:32
  • require.context(images should probably be require.context('./images/wd/' Commented Oct 12, 2020 at 20:53
  • @Dominik. I have tried many of those methods and didn't work at all. There is no webpack.config.js in the latest versions. Commented Oct 12, 2020 at 21:09
  • @cbr, the path was originally as you described, but even so, the error of "Module not found: Can't resolve './images/wd/' in '/mywebsite/src/components'" still persists. Commented Oct 12, 2020 at 21:10
  • what is "/wd/" in the images path? Commented Oct 12, 2020 at 21:11

1 Answer 1

1

Here is the correct answer Dynamically import images from a directory using webpack You cannot import the whole directory at the same time with only "import"

    function importAll(r) {
      return r.keys().map(r);
    }
    
    const images = importAll(
      require.context("./images/wd", false, /\.(png|jpe?g|svg)$/)
    );

UPD: So in your case remove import, swap images with url and add listOfImages to the state

import React, { Component } from "react";

class Myloop extends Component {
  constructor(props) {
    super(props);
    this.state = { listOfImages: [] };
  }
  importAll(r) {
    return r.keys().map(r);
  }

  componentWillMount() {
    const list = this.importAll(
      require.context("./images/wd", false, /\.(png)$/)
    );
    this.setState({
      listOfImages: list
    });
  }

  render() {
    return (
      <>
        <ul>
          <li>
            {this.state.listOfImages.map((image, index) => (
              <img src={image} key={index} alt="info"></img>
            ))}
          </li>
        </ul>
      </>
    );
  }
}

export default Myloop;
Sign up to request clarification or add additional context in comments.

3 Comments

That's the right one. Thanks @Julka. I implemented and it works like a charm.
Although, if my images folder is outside the components folder, it doesn't load. Any idea?
@ffas, change path from "./images/wd" to needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.