eiriklv / react-masonry-component

A React.js component for using @desandro's Masonry
MIT License
1.44k stars 145 forks source link

Unbind imagesLoaded event #120

Closed y310 closed 6 years ago

y310 commented 6 years ago

When masonry component is unmounted before finishing all imagesloaded callback, the callback still triggers the bound function. In my case, since I called setState in the function, I got a following warning.

Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

In order to fix this issue, I added unbinding code to componentWillUnmount.

afram commented 6 years ago

Hi @y310 Thanks for this.

I'm currently looking at the code and believe that the repeated calls to imagesLoaded is actually a mistake.

this.imagesLoaded() should be called once on mounting only, so there should only be a single ref (which will simplify this PR).

I'm sorry to ask, are you interested in submitting another PR once i finish the changes? Otherwise I can do it.

afram commented 6 years ago

edit: ignore me. Looks like imagesloaded does not listen for images added after it's been initialised.

y310 commented 6 years ago

Thanks for merging! Yeah, seems like imagesloaded requires to be called on update as well.