Skip to content

Commit c5b44df

Browse files
committed
fix: Should not crash when height change
1 parent ef24fe9 commit c5b44df

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

examples/switch.tsx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function getData(count: number) {
3434
}
3535

3636
const Demo = () => {
37-
const [state, setState] = React.useState<number>(0);
37+
const [height, setHeight] = React.useState(200);
38+
const [state, setState] = React.useState<number>(1);
3839

3940
const data = React.useMemo(() => {
4041
switch (state) {
@@ -51,9 +52,37 @@ const Demo = () => {
5152
<React.StrictMode>
5253
<div>
5354
<h2>Switch</h2>
55+
56+
<button
57+
type="button"
58+
onClick={() => {
59+
setState((state + 1) % 3);
60+
}}
61+
>
62+
Switch
63+
</button>
64+
<button
65+
type="button"
66+
onClick={() => {
67+
switch (height) {
68+
case 200:
69+
setHeight(0);
70+
break;
71+
case 0:
72+
setHeight(100);
73+
break;
74+
default:
75+
setHeight(200);
76+
break;
77+
}
78+
}}
79+
>
80+
Height
81+
</button>
82+
5483
<List
5584
data={data}
56-
height={200}
85+
height={height}
5786
itemHeight={30}
5887
itemKey="id"
5988
style={{
@@ -63,14 +92,6 @@ const Demo = () => {
6392
>
6493
{(item, _, props) => <ForwardMyItem {...item} {...props} />}
6594
</List>
66-
<button
67-
type="button"
68-
onClick={() => {
69-
setState((state + 1) % 3);
70-
}}
71-
>
72-
Switch
73-
</button>
7495
</div>
7596
</React.StrictMode>
7697
);

src/List.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
361361
relativeTop: originCompareItemTop,
362362
});
363363
}
364+
} else if (nextStatus === 'SWITCH_TO_RAW') {
365+
// This is only trigger when height changes that all items can show in raw
366+
// Let's reset back to top
367+
this.setState({
368+
cacheScroll: {
369+
itemIndex: 0,
370+
relativeTop: 0,
371+
},
372+
});
364373
}
365374

366375
this.cachedProps = this.props;

tests/list.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,9 @@ describe('List', () => {
191191
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), virtual: false });
192192
expect(wrapper.find('li')).toHaveLength(100);
193193
});
194+
195+
it('Should not crash when height change makes virtual scroll to be raw scroll', () => {
196+
const wrapper = genList({ itemHeight: 20, height: 40, data: genData(3) });
197+
wrapper.setProps({ height: 1000 });
198+
});
194199
});

0 commit comments

Comments
 (0)