Skip to content

Commit ebf3dda

Browse files
committed
Add more v1 -> v2 migration examples
1 parent 4008a09 commit ebf3dda

File tree

1 file changed

+86
-4
lines changed

1 file changed

+86
-4
lines changed

CHANGELOG.md

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function RowComponent({ index, style, ...rest }: RowComponentProps<object>) {
9393
}
9494
```
9595

96-
## 2.0.0
96+
# 2.0.0
9797

9898
Version 2 is a major rewrite that offers the following benefits:
9999

@@ -105,9 +105,11 @@ Version 2 is a major rewrite that offers the following benefits:
105105

106106
## Upgrade path
107107

108-
See the [documentation](https://react-window.vercel.app/) for more details, but here is an example of what the v1 to v2 upgrade might look like:
108+
This section contains a couple of examples for common upgrade paths. Please refer to the [documentation](https://react-window.vercel.app/) for more information.
109109

110-
### Before
110+
### Migrating `FixedSizeList`
111+
112+
#### Before
111113

112114
```tsx
113115
import { FixedSizeList, type ListChildComponentProps } from "react-window";
@@ -140,12 +142,14 @@ function Row({
140142
}
141143
```
142144

143-
### After
145+
#### After
144146

145147
```tsx
146148
import { List, type RowComponentProps } from "react-window";
147149

148150
function Example({ names }: { names: string[] }) {
151+
// You don't need to useMemo for rowProps;
152+
// List will automatically memoize them
149153
return (
150154
<List
151155
rowComponent={RowComponent}
@@ -168,6 +172,84 @@ function RowComponent({
168172
}
169173
```
170174

175+
### Migrating `VariableSizedList`
176+
177+
#### Before
178+
179+
```tsx
180+
import { VariableSizeList, type ListChildComponentProps } from "react-window";
181+
182+
function Example({ items }: { items: Item[] }) {
183+
const itemData = useMemo<ItemData>(() => ({ items }), [items]);
184+
const itemSize = useCallback(
185+
(index: number) => {
186+
const item = itemData.items[index];
187+
return item.type === "header" ? 40 : 20;
188+
},
189+
[itemData]
190+
);
191+
192+
return (
193+
<VariableSizeList
194+
children={Row}
195+
height={150}
196+
itemCount={1000}
197+
itemData={itemData}
198+
itemSize={itemSize}
199+
width={300}
200+
/>
201+
);
202+
}
203+
204+
function itemSize();
205+
206+
function Row({
207+
data,
208+
index,
209+
style
210+
}: ListChildComponentProps<{
211+
items: Item[];
212+
}>) {
213+
const { items } = data;
214+
const item = items[index];
215+
return <div style={style}>{item.label}</div>;
216+
}
217+
```
218+
219+
#### After
220+
221+
```tsx
222+
import { List, type RowComponentProps } from "react-window";
223+
224+
type RowProps = {
225+
items: Item[];
226+
};
227+
228+
function Example({ items }: { items: Item[] }) {
229+
// You don't need to useMemo for rowProps;
230+
// List will automatically memoize them
231+
return (
232+
<List
233+
rowComponent={RowComponent}
234+
rowCount={items.length}
235+
rowHeight={rowHeight}
236+
rowProps={{ items }}
237+
/>
238+
);
239+
}
240+
241+
// The rowHeight method also receives the extra props,
242+
// so it can be defined at the module level
243+
function rowHeight(index: number, { item }: RowProps) {
244+
return item.type === "header" ? 40 : 20;
245+
}
246+
247+
function RowComponent({ index, items, style }: RowComponentProps<RowProps>) {
248+
const item = items[index];
249+
return <div style={style}>{item.label}</div>;
250+
}
251+
```
252+
171253
### ⚠️ Version 2 requirements
172254

173255
The following requirements are new in version 2 and may be reasons to consider _not_ upgrading:

0 commit comments

Comments
 (0)