I have dropdown list with a lot of checkboxes, so this container has scroll. But when i click on any checkbox - it selects\deselects itself and then state changes.
So the problem is that after rerendering this container is back to the top. Is it possible to keep container's scroll after rendering without saving it to the state?
By default you can set the value of scrollPosition is 0 . Here I have used the sessionStorage to maintain the scroll position for demo purpose. You can also use the context API or redux store to manage it.
You can try $("div#element"). load(function() {}) and place the element outside the document ready handler. Also, if a user scroll down the page, then leave the page, and go back to the page again, it should be a new start for him so it should start at the top. But now it also remembers the previous position.
In Flatlist set scrollsToTop={false} this will retain position when you navigate back from detail screen.
You can save the "snapshot" of the scroll position before the commit phase.
getSnapshotBeforeUpdate() shows kind of what you are looking for.
The documentation example saves the current scroll position within getSnapshotBeforeUpdate
lifecycle method and then use the snapshot
value passed to componentDidUpdate(prevProps, prevState, snapshot)
as the last argument to restore the scroll position.
It doesn't require creating a state to save the scroll position as you requested.
It's happening because you are re-rendering the complete list of the checkboxes.
There are 2 possible approaches:
Unfortunately, you haven't added any code examples, so can't share the code changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With