Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

javascript - Displaying all images in a folder in React using webpack shows broken icons

I'm working on an application in React and I want to display all images in a folder. I'm using webpack and I have this structure:

->src
   ->image_display (folder containing the React component I want to use and a folder with the images)
            image.js 
            ->images
                 Image01.jpg
                 Image02.jpg
                 Image03.jpg

My code is

class Images extends Component{

constructor(props) {
        super(props);
        this.state = {
            ListOfImages: []
       }
}


importAll(r) {
        return r.keys().map(r);
    }

componentDidMount() {
        let ListOfImages = this.importAll(require.context('./images/', false, /.(png|jpe?g|svg)$/));
        console.log(ListOfImages)
        this.setState({ListOfImages:ListOfImages})
    }

 render() {
        return(
            <div>
                {
                    this.state.ListOfImages.map(
                        (image, index) =>    <img key={index} src={image} alt="Image"></img>
                    )
                }
            </div>
        )

 }

}

Instead of images React shows broken icons. What can be the problem? Can the path be wrong? Thank you!

question from:https://stackoverflow.com/questions/65884522/displaying-all-images-in-a-folder-in-react-using-webpack-shows-broken-icons

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...