Skip to content

Commit 9741f0c

Browse files
committed
fix: retry key scroll after data update
1 parent 8981cf6 commit 9741f0c

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/hooks/useScrollTo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default function useScrollTo<T>(
9191
const offset = getOffset(rawOffset, { getSize, align: mergedAlign });
9292

9393
const height = containerRef.current.clientHeight;
94-
let needCollectHeight = false;
94+
let needCollectHeight = index < 0;
9595
let newTargetAlign: 'top' | 'bottom' | null = targetAlign;
9696
let targetTop: number | null = null;
9797

tests/scroll.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,24 @@ describe('List.Scroll', () => {
208208
expect(container.querySelector('ul').scrollTop).toEqual(520);
209209
});
210210

211-
it('refreshes key index when data changes', () => {
211+
it('retries key scroll when data changes after layout', () => {
212212
const ref = React.createRef();
213213

214214
function Demo() {
215215
const [data, setData] = React.useState(genData(1));
216+
const [update, setUpdate] = React.useState(false);
217+
218+
React.useLayoutEffect(() => {
219+
if (update) {
220+
setData(genData(100));
221+
}
222+
}, [update]);
216223

217224
return (
218225
<>
219226
<button
220227
onClick={() => {
221-
setData(genData(100));
228+
setUpdate(true);
222229
ref.current.scrollTo({ key: '30', align: 'top' });
223230
}}
224231
/>

0 commit comments

Comments
 (0)