You do not need to do this in all of your components. Once the image is uploaded, it will be cached by the browser and will be available in all components, so you can only do this once in a high-level component.
I don’t know what UX you are trying to create by caching images, however, your code initiates the loading of images, but does not know if the image is loading, whether it was loaded successfully or even failed. So, for example, you want to show a button for changing images or add a class to a component only when the images have been loaded (to make them smooth), your current code may let you down.
You can solve this problem with Promises.
// create an utility function somewhere const checkImage = path => new Promise(resolve => { const img = new Image() img.onload = () => resolve(path) img.onerror = () => reject() img.src = path }) ... // then in your component constructor(props) { super(props) this.state = { imagesLoaded: false } } // doesn't really matter whether you do it in componentWillMount or componentDidMount componentWillMount() { Promise.all( R.take(limit, imgUrls) .map(checkImage) ).then(() => this.setState({ imagesLoaded: true }), () => console.error('could not load images')) } render = () => this.state.imagesLoaded ? <BeautifulComponent /> : <Skeleton />
As for memory consumption - I do not think that something bad will happen. Browsers usually limit the number of concurrent xhr requests, so you won’t be able to create a giant burst of heap usage to destroy anything, since unused images will collect garbage (they are still stored in the browser’s cache).
The Redux store is a place to store the state of the application, not the assets of the application, but in any case, you will not be able to store any real images there.
source share