Skip to content

Commit 1be5020

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

File tree

1 file changed

+157
-4
lines changed

1 file changed

+157
-4
lines changed

CHANGELOG.md

Lines changed: 157 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,155 @@ 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+
253+
### Migrating `FixedSizeGrid`
254+
255+
#### Before
256+
257+
```tsx
258+
import { FixedSizeGrid, type GridChildComponentProps } from "react-window";
259+
260+
function Example({ data }: { data: Data[] }) {
261+
const itemData = useMemo<ItemData>(() => ({ data }), [data]);
262+
263+
return (
264+
<FixedSizeGrid
265+
children={Cell}
266+
columnCount={data[0]?.length ?? 0}
267+
columnWidth={100}
268+
height={150}
269+
itemData={itemData}
270+
rowCount={data.length}
271+
rowHeight={35}
272+
width={300}
273+
/>
274+
);
275+
}
276+
277+
function Cell({
278+
columnIndex,
279+
data,
280+
rowIndex,
281+
style
282+
}: GridChildComponentProps<{
283+
names: string[];
284+
}>) {
285+
const { data } = data;
286+
const datum = data[index];
287+
return <div style={style}>...</div>;
288+
}
289+
```
290+
291+
#### After
292+
293+
```tsx
294+
import { FixedSizeGrid, type GridChildComponentProps } from "react-window";
295+
296+
function Example({ data }: { data: Data[] }) {
297+
// You don't need to useMemo for cellProps;
298+
// Grid will automatically memoize them
299+
return (
300+
<Grid
301+
cellComponent={Cell}
302+
cellProps={{ data }}
303+
columnCount={data[0]?.length ?? 0}
304+
columnWidth={columnWidth}
305+
rowCount={data.length}
306+
rowHeight={25}
307+
/>
308+
);
309+
}
310+
311+
function Cell({
312+
columnIndex,
313+
data,
314+
rowIndex,
315+
style
316+
}: CellComponentProps<{
317+
data: Data[];
318+
}>) {
319+
const datum = data[rowIndex][columnIndex];
320+
return <div style={style}>...</div>;
321+
}
322+
```
323+
171324
### ⚠️ Version 2 requirements
172325

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

0 commit comments

Comments
 (0)