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 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 usesuseEffect
withneedsFreshBatch
dependency. WhenneedsFreshBatch
stays true between renders this hook isn't called. When positioner changes, there is a new instance ofsetItemRef
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 touseEffect
dependencies fixes this issue - it causes another update.My guess is when
itemHeightEstimate
is accurate,needsFreshBatch
changes fromtrue
tofalse
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.