petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.16k stars 299 forks source link

[BUG] Incorrect scrollTo in TableVirtuoso with let top #1007

Closed alex-nb closed 10 months ago

alex-nb commented 10 months ago

Describe the bug

let top = 0;

tableRef.current.getState(({ scrollTop }) => {
  top = scrollTop;
});

tableRef.current.scrollTo({
  left: 0,
  top,
  behavior: "smooth"
});

scrollTo doesn't work in that case. It still works without top or without assignment in getState

Reproduction Codesandbox

To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/runtime-flower-pk4wqq?file=/App.js
  2. Scroll right and down
  3. Click on 'Scroll' button
  4. Nothing happened

Expected behavior Scroll is working

Desktop (please complete the following information):

petyosi commented 10 months ago

What are you trying to do here? Reset horizontal scrolling? If so, using the virtuoso API makes no sense, get the element with scrollerRef and use the DOM API, I think that you can even omit the top value, and just pass the left one.

alex-nb commented 10 months ago

Yes, I already used this solution, but still it can be bug

petyosi commented 10 months ago

I see your point. At the moment, the component methods have guards against scrolling to the same vertical location, and don't take the horizontal one into account.

However, calling getState is fairly expensive operation, so I would not recommend that approach even if it worked.