Skip to content

Commit 0727e22

Browse files
authored
Add more v1 -> v2 migration examples (#848)
1 parent 4008a09 commit 0727e22

File tree

1 file changed

+256
-4
lines changed

1 file changed

+256
-4
lines changed

CHANGELOG.md

Lines changed: 256 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,254 @@ 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={75}
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+
324+
### Migrating `VariableSizeGrid`
325+
326+
#### Before
327+
328+
```tsx
329+
import { VariableSizeGrid, type GridChildComponentProps } from "react-window";
330+
331+
function Example({ data }: { data: Data[] }) {
332+
const itemData = useMemo<ItemData>(() => ({ data }), [data]);
333+
334+
const columnWidth = useCallback(
335+
(columnIndex: number) => {
336+
// ...
337+
},
338+
[itemData]
339+
);
340+
341+
const rowHeight = useCallback(
342+
(rowIndex: number) => {
343+
// ...
344+
},
345+
[itemData]
346+
);
347+
348+
return (
349+
<VariableSizeGrid
350+
children={Cell}
351+
columnCount={data[0]?.length ?? 0}
352+
columnWidth={columnWidth}
353+
height={150}
354+
itemData={itemData}
355+
rowCount={data.length}
356+
rowHeight={rowHeight}
357+
width={300}
358+
/>
359+
);
360+
}
361+
362+
function Cell({
363+
columnIndex,
364+
data,
365+
rowIndex,
366+
style
367+
}: GridChildComponentProps<{
368+
names: string[];
369+
}>) {
370+
const { data } = data;
371+
const datum = data[index];
372+
return <div style={style}>...</div>;
373+
}
374+
```
375+
376+
#### After
377+
378+
```tsx
379+
import { FixedSizeGrid, type GridChildComponentProps } from "react-window";
380+
381+
type CellProps = {
382+
data: Data[];
383+
};
384+
385+
function Example({ data }: { data: Data[] }) {
386+
// You don't need to useMemo for cellProps;
387+
// Grid will automatically memoize them
388+
return (
389+
<Grid
390+
cellComponent={Cell}
391+
cellProps={{ data }}
392+
columnCount={data[0]?.length ?? 0}
393+
columnWidth={columnWidth}
394+
rowCount={data.length}
395+
rowHeight={rowHeight}
396+
/>
397+
);
398+
}
399+
400+
// The columnWidth method also receives the extra props,
401+
// so it can be defined at the module level
402+
function columnWidth(columnIndex: number, { data }: CellProps) {
403+
// ...
404+
}
405+
406+
// The rowHeight method also receives the extra props,
407+
// so it can be defined at the module level
408+
function rowHeight(rowIndex: number, { data }: CellProps) {
409+
// ...
410+
}
411+
412+
function Cell({
413+
columnIndex,
414+
data,
415+
rowIndex,
416+
style
417+
}: CellComponentProps<CellProps>) {
418+
const datum = data[rowIndex][columnIndex];
419+
return <div style={style}>...</div>;
420+
}
421+
```
422+
171423
### ⚠️ Version 2 requirements
172424

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

0 commit comments

Comments
 (0)