Skip to content

Commit

Permalink
fix: dynamic size not working properly when working with RWD
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jun 4, 2021
1 parent e100cd0 commit b8445d8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-cameras-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

fix: dynamic size not working properly when working with RWD
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ const List = () => {
};
```

> 馃挕 If the item size is specified through the function of `itemSize`, please ensure there's no the [measureRef](#items) on the item element. Otherwise, the hook will use the measured (cached) size for the item. When working with RWD, we can only use either of the two.
### Scroll to Offset/Items

You can imperatively scroll to offset or items as follows:
Expand Down
6 changes: 4 additions & 2 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,15 @@ export default <
msDataRef.current[msDataRef.current.length - 1]?.end;

outerRectRef.current = rect;
measureItems(isSameWidth);
measureItems(hasDynamicSizeRef.current || isSameWidth);
handleScroll(scrollOffsetRef.current);

if (onResizeRef.current) onResizeRef.current(rect);

if (hasDynamicSizeRef.current || isSameWidth) return;

const totalSize = msDataRef.current[msDataRef.current.length - 1]?.end;
const ratio = !isSameWidth && totalSize / prevTotalSize;
const ratio = totalSize / prevTotalSize;

if (ratio) scrollTo(scrollOffsetRef.current * ratio);
},
Expand Down

0 comments on commit b8445d8

Please sign in to comment.