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

Preseving item state after scrolling away #141

Closed
itayganor opened this issue Jul 12, 2020 · 9 comments
Closed

Preseving item state after scrolling away #141

itayganor opened this issue Jul 12, 2020 · 9 comments

Comments

@itayganor
Copy link

Hi :)

Just wanted to say thank you for the most simple virtualization library I've ever seen. I've been through react-virtualized and react-window, and this one is just heaven.

I do have one question though:
When I expand one of my list items, It won't be expanded the next time it will be mounted.
In other words - it seems like I lose the component's state when it's out of view.

Here's an example:

d3dd7770-b74f-4277-8aad-f99837e3b133

As you can see, the default state is collapsed. I expand one of the items, scroll down, and scroll back up to make it inside the view again, and it's collapsed again. You can see the scrollbar jumping a few pixels when the list item gets remounted with a wrong height (collapsed instead of expanded, but the virtual list remembers the last height).

I couldn't find anything related to this on docs.

How can this be solved? I imagine some kind of memoization, but I couldn't make it work.

Thanks!

@vinujan59
Copy link

storing the expanded state in list’s parent would be a solution. Propagate the expanded event and store it in object or something and pass down the expanded state to list element.

@itayganor
Copy link
Author

itayganor commented Jul 12, 2020

storing the expanded state in list’s parent would be a solution. Propagate the expanded event and store it in object or something and pass down the expanded state to list element.

This is indeed a good solution, but the thing is the state might contain more than just the expansion state. I'm actually trying to achieve a generic component that will accept any kind of item as a child, and remember each one's state when it's not visible.

Passing a state object to the parent is still a valid solution, but I'm looking for something a bit more independent

@fonty422
Copy link

It's not possible to have it stored in the object its self. You have two choices:

  1. On the onExpand event sends something back to the parent which holds a list of what is expanded which is parsed to each list item on render. It does require you to have a means to know which things are expanded and closed - and that should NOT be the list index.
  2. Use something like React-Redux to store not only the general information to display, but also the expanded prop. You can default it to closed, but when you expand it modifies the expanded prop to true. That way the object kind of does keep track of its own expanded state... via a store.

The second one is definitely my preferred option as all my data for the entire app is already held in a redux store.
Perhaps, though, there might be the possibility of someone more cleaver than I to develop a better, more permanent and independent solution.

@petyosi
Copy link
Owner

petyosi commented Jan 26, 2021

I second the suggestions provided here - the item state should be preserved externally, using the state management of your choice.

@petyosi petyosi closed this as completed Jan 26, 2021
@pyreta
Copy link

pyreta commented Mar 4, 2021

@itayganor Can I see the code for this expandable list you made?

@fonty422
Copy link

fonty422 commented Mar 8, 2021

As in just for the list, or including the redux part?

@theconnectionist
Copy link

As in just for the list, or including the redux part?

@fonty422 Just for the list would be good enough, but redux would be a nice-to-have :-)

@fonty422
Copy link

fonty422 commented Jun 9, 2021

Here's a basic sandbox complete with a basic redux set up.
If you're not familiar with using redux, this might appear a bit confusing and I recommend a good tutorial - perhaps even a tutorial series. I've tried to add plenty of comments where I think they're needed for clarification.

@theconnectionist
Copy link

theconnectionist commented Jun 14, 2021

Thank you fonty422. Much appreciated. I see the Accordion control is a bit cleaner to use than the Collapse control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants