nikitaeverywhere / react-xmasonry

Simple, minimalistic and featured native masonry layout for React JS.
https://zitros.github.io/react-xmasonry
MIT License
91 stars 12 forks source link

Graceful handling of empty collection #6

Closed aogaili closed 6 years ago

aogaili commented 6 years ago

Hello!

As of now , XMasonry always assumes the presence of children blocks. If you insert the following tags:

<XMasonry></XMasonry>

without any child blocks, you'll get the following error:

Uncaught TypeError: Cannot read property 'length' of undefined
    at e.value (modules.js?hash=b342853…:32166)
    at modules.js?hash=b342853…:18227
    at measureLifeCyclePerf (modules.js?hash=b342853…:17507)
    at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (modules.js?hash=b342853…:18226)
    at ReactCompositeComponentWrapper._renderValidatedComponent (modules.js?hash=b342853…:18253)
    at ReactCompositeComponentWrapper._updateRenderedComponent (modules.js?hash=b342853…:18177)
    at ReactCompositeComponentWrapper._performComponentUpdate (modules.js?hash=b342853…:18155)
    at ReactCompositeComponentWrapper.updateComponent (modules.js?hash=b342853…:18076)
    at ReactCompositeComponentWrapper.performUpdateIfNecessary (modules.js?hash=b342853…:17992)
    at Object.performUpdateIfNecessary (modules.js?hash=b342853…:10561)

I personally think it'd be a better developer experience if XMasonry handle this case gracefully (without throwing errors). It's very likely that the children blocks are dynamically created and the collection would be initially empty, which'll result in the above error.

Let me know what you think @ZitRos

nikitaeverywhere commented 6 years ago

Nice catch, @aliogaili! :)

However,

<XMasonry>{ [] }</XMasonry>

Works nicely. Let's see...

nikitaeverywhere commented 6 years ago

Now fixed in 2.4.2.

aogaili commented 6 years ago

Thank you so much @ZitRos for the quick fixes, working well now :)