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

Using ItemContainer and useState causes rerender #53

Closed
NerdCowboy opened this issue Nov 5, 2019 · 3 comments
Closed

Using ItemContainer and useState causes rerender #53

NerdCowboy opened this issue Nov 5, 2019 · 3 comments
Labels
question Further information is requested

Comments

@NerdCowboy
Copy link

I'm using a GroupedVirtuoso with buttons in each row. To make my list more accessible, I'm trying to keep track of the focused index, so I can auto-scroll the list as a user tabs through it.

However, In order to properly style my list elements, I needed to add an ItemContainer. Once I add the ItemContainer in combination with setFocusedIndex via useState, it will always cause a rerender of the list once I try to tab past the first button and thus can't tab anywhere past the first row.

CodeSandbox with reproduced the issue:
https://codesandbox.io/s/heuristic-dijkstra-ml1sj

@petyosi
Copy link
Owner

petyosi commented Nov 6, 2019

The way I understand useState hooks, the behavior is expected. you call setState, and changing the state will re-render the container.

If I understand your requirements correctly, I believe that there is a simpler way to achieve what you describe - using scrollIntoView. Check this fork of the sandbox for what I mean.

@petyosi petyosi added the question Further information is requested label Nov 6, 2019
@NerdCowboy
Copy link
Author

I had a suspicion that might be the case case. I really appreciate you proving a better solution—wasn't even aware that scrollIntoView was thing.

@NerdCowboy
Copy link
Author

For others that might still be running into a similar issue, my component was a little more complex than the sandbox I created, so I had to also wrap my ItemContainer in a useCallback to prevent it from causing rerenders that would cause it to lose focus.

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