Keep scroll-y after rerender

后端 未结 2 1224
既然无缘
既然无缘 2021-01-04 09:13

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.

相关标签:
2条回答
  • 2021-01-04 09:50

    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.

    0 讨论(0)
  • 2021-01-04 10:00

    It's happening because you are re-rendering the complete list of the checkboxes.

    There are 2 possible approaches:

    1. Re-render only relevant checkbox
    2. Save the scroll position of the container and update it once component is re-rendered.

    Unfortunately, you haven't added any code examples, so can't share the code changes.

    0 讨论(0)
提交回复
热议问题