I’m having a difficult time figuring out when images in a component are loaded and ready for DOM manipulation. I wrote my own mixin (below) which works fine over fast connections, but is unpredictable on a slower connection. Of course it’s actually fetching all the images on the page, and I’d rather limit it to just those in the component, but I’m not sure how to accomplish that. Any advice would be greatly appreciated!
The mixin would provide a method on the component called waitForLoad, and you’d use it like this:
<img ref={this.waitForLoad} />
One unfortunate side effect is that you need to attach a ref to each image, but the upside is now you get to control which images you’re waiting for.
Another approach would be using context and a custom <ImageLoading> component, in which case you wouldn’t need to specify the ref, and it would work with all of the descendants of the component.
Oh nice, I didn’t realize a ref could be a callback! I’ll go that route rather than using context. React devs have mentioned the possibility of removing context soon, so I don’t want to rely on it.
Amazing! I’m really excited that we’re coming up with new and cool ways to do stuff with React. I also just learned about the ref callback thing, it’s a neat feature that has tons of possible applications.
I didn’t realize you could use a callback either, till you mentioned it.
I’ll make this into a Meteor package soon, because I think this is really handy to have. It’s not the first time I’ve run into a situation where I needed to wait for images to be loaded before carrying out some other action.