Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Item state to a smaller list of items #158

Open
justisom opened this issue Apr 26, 2024 · 2 comments
Open

Updating Item state to a smaller list of items #158

justisom opened this issue Apr 26, 2024 · 2 comments
Labels
question Further information is requested

Comments

@justisom
Copy link

Describe the bug
Item state changes that reduce the list size causing a failure:
This usually happens when you've mutated or changed the "items" array in a way that makes it shorter than the previous "items" array. Masonic knows nothing about your underlying data and when it caches cell positions, it assumes you aren't mutating the underlying "items".

To Reproduce
Steps to reproduce the behavior:

Using MyMasonry in this way:
useMasonry({ positioner: positioner, resizeObserver, items, height, scrollTop, isScrolling, overscanBy: 6, render: Item })
where items is a stateful list that can grow or shrink

Expected behavior
In my case I'm using a single page with table pagination that can display a variable maximum amount of items per page. So if I were to choose 50 when I'm looking at 100, I'd like it to be able to re-draw the whole thing. That doesn't seem to be happening.
Also this is an issue when I start using filtering.

I don't think the provided examples
See https://codesandbox.io/s/masonic-w-react-router-example-2b5f9?file=/src/index.js for an example that gets around this limitations. For advanced implementations, see https://codesandbox.io/s/masonic-w-react-router-and-advanced-config-example-8em42?file=/src/index.js
adequately address the issue that I'm facing since I'm not changing my route in this case. I'm simply updating state and re-rendering my div.

Happy to clarify further.

Thanks!

@jaredLunde
Copy link
Owner

The examples actually do address the issue. You either need to use a key prop to reset the component or provide a dependency array to the positioner. So in your case, you'd do that after you update the state/re-render. e.g.

usePositioner({...}, [itemsPerPage])

@justisom
Copy link
Author

usePositioner({...}, [itemsPerPage])

That's the part I missed. Thanks so much!

@jaredLunde jaredLunde added the question Further information is requested label Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants