diff --git a/CHANGELOG.md b/CHANGELOG.md index ce857f3a..b16ef431 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,7 +93,7 @@ function RowComponent({ index, style, ...rest }: RowComponentProps) { } ``` -## 2.0.0 +# 2.0.0 Version 2 is a major rewrite that offers the following benefits: @@ -105,9 +105,11 @@ Version 2 is a major rewrite that offers the following benefits: ## Upgrade path -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: +This section contains a couple of examples for common upgrade paths. Please refer to the [documentation](https://react-window.vercel.app/) for more information. -### Before +### Migrating `FixedSizeList` + +#### Before ```tsx import { FixedSizeList, type ListChildComponentProps } from "react-window"; @@ -140,12 +142,14 @@ function Row({ } ``` -### After +#### After ```tsx import { List, type RowComponentProps } from "react-window"; function Example({ names }: { names: string[] }) { + // You don't need to useMemo for rowProps; + // List will automatically memoize them return ( (() => ({ items }), [items]); + const itemSize = useCallback( + (index: number) => { + const item = itemData.items[index]; + return item.type === "header" ? 40 : 20; + }, + [itemData] + ); + + return ( + + ); +} + +function itemSize(); + +function Row({ + data, + index, + style +}: ListChildComponentProps<{ + items: Item[]; +}>) { + const { items } = data; + const item = items[index]; + return
{item.label}
; +} +``` + +#### After + +```tsx +import { List, type RowComponentProps } from "react-window"; + +type RowProps = { + items: Item[]; +}; + +function Example({ items }: { items: Item[] }) { + // You don't need to useMemo for rowProps; + // List will automatically memoize them + return ( + + ); +} + +// The rowHeight method also receives the extra props, +// so it can be defined at the module level +function rowHeight(index: number, { item }: RowProps) { + return item.type === "header" ? 40 : 20; +} + +function RowComponent({ index, items, style }: RowComponentProps) { + const item = items[index]; + return
{item.label}
; +} +``` + +### Migrating `FixedSizeGrid` + +#### Before + +```tsx +import { FixedSizeGrid, type GridChildComponentProps } from "react-window"; + +function Example({ data }: { data: Data[] }) { + const itemData = useMemo(() => ({ data }), [data]); + + return ( + + ); +} + +function Cell({ + columnIndex, + data, + rowIndex, + style +}: GridChildComponentProps<{ + names: string[]; +}>) { + const { data } = data; + const datum = data[index]; + return
...
; +} +``` + +#### After + +```tsx +import { FixedSizeGrid, type GridChildComponentProps } from "react-window"; + +function Example({ data }: { data: Data[] }) { + // You don't need to useMemo for cellProps; + // Grid will automatically memoize them + return ( + + ); +} + +function Cell({ + columnIndex, + data, + rowIndex, + style +}: CellComponentProps<{ + data: Data[]; +}>) { + const datum = data[rowIndex][columnIndex]; + return
...
; +} +``` + +### Migrating `VariableSizeGrid` + +#### Before + +```tsx +import { VariableSizeGrid, type GridChildComponentProps } from "react-window"; + +function Example({ data }: { data: Data[] }) { + const itemData = useMemo(() => ({ data }), [data]); + + const columnWidth = useCallback( + (columnIndex: number) => { + // ... + }, + [itemData] + ); + + const rowHeight = useCallback( + (rowIndex: number) => { + // ... + }, + [itemData] + ); + + return ( + + ); +} + +function Cell({ + columnIndex, + data, + rowIndex, + style +}: GridChildComponentProps<{ + names: string[]; +}>) { + const { data } = data; + const datum = data[index]; + return
...
; +} +``` + +#### After + +```tsx +import { FixedSizeGrid, type GridChildComponentProps } from "react-window"; + +type CellProps = { + data: Data[]; +}; + +function Example({ data }: { data: Data[] }) { + // You don't need to useMemo for cellProps; + // Grid will automatically memoize them + return ( + + ); +} + +// The columnWidth method also receives the extra props, +// so it can be defined at the module level +function columnWidth(columnIndex: number, { data }: CellProps) { + // ... +} + +// The rowHeight method also receives the extra props, +// so it can be defined at the module level +function rowHeight(rowIndex: number, { data }: CellProps) { + // ... +} + +function Cell({ + columnIndex, + data, + rowIndex, + style +}: CellComponentProps) { + const datum = data[rowIndex][columnIndex]; + return
...
; +} +``` + ### ⚠️ Version 2 requirements The following requirements are new in version 2 and may be reasons to consider _not_ upgrading: