Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to preload images in React.js?

/*
Supposing you have pictures: string[];
- array of pictures urls defined in your component's props.
You should define componentDidMount() 
method in your component and then just create new Image object for each picture you want to be preloaded:
*/
componentDidMount() {
    this.props.pictures.forEach((picture) => {
        const img = new Image();
        img.src = picture.fileName;
    });
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #preload #images
ADD COMMENT
Topic
Name
2+3 =