jaredLunde / masonic

🧱 High-performance masonry layouts for React
https://codesandbox.io/s/0oyxozv75v
MIT License
797 stars 49 forks source link

fix(use-masonry): should update when positioner changes #113

Closed V3RON closed 2 years ago

V3RON commented 2 years ago

This pull request fixes an issue around grid invisibility when itemHeightEstimate is not accurate and items get mutated.

The reason why this bug is present: When itemHeightEstimate is not accurate, there are multiple batches rendered one by one. When items get mutated, new positioner instance is created and it should be filled with entries describing all visible tiles. Unfortunately, it doesn't happen. useMasonry hook is stuck in stage 1 forever unless its parent updates triggering stage 2.

To update itself, useMasonry hook uses useEffect with needsFreshBatch dependency. When needsFreshBatch stays true between renders this hook isn't called. When positioner changes, there is a new instance of setItemRef function created, which is responsible for populating cache. Now, there is another update needed for stage 2 to take place. That's why adding positioner to useEffect dependencies fixes this issue - it causes another update.

My guess is when itemHeightEstimate is accurate, needsFreshBatch changes from true to false properly. That's why this bug isn't common.

How it was fixed: positioner got added to dependency array of the force update effect. This leads to another update populating cache and trigger stage 2.

This pull request closes #112.

wjdwndud0114 commented 2 years ago

LGTM

jaredLunde commented 2 years ago

Appreciate it

github-actions[bot] commented 2 years ago

:tada: This PR is included in version 3.6.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket: