-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add VirtualColumn and VirtualRow docs #4845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lunaleaps
wants to merge
3
commits into
facebook:main
Choose a base branch
from
lunaleaps:virtual-column-row
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| --- | ||
| id: virtualcolumn | ||
| title: VirtualColumn 🧪 | ||
| --- | ||
|
|
||
| import ExperimentalAPIWarning from './\_experimental-api-warning.mdx'; | ||
|
|
||
| <ExperimentalAPIWarning /> | ||
|
|
||
| `VirtualColumn` is a column layout component that lazily mounts and virtualizes items. | ||
|
|
||
| ```tsx | ||
| <ScrollView> | ||
| <VirtualColumn | ||
| items={new VirtualArray(items)}> | ||
| {(item, key) => <Text>{item.value}</Text>} | ||
| </VirtualColumn> | ||
| </ScrollView> | ||
| ``` | ||
|
|
||
| A `ScrollView` ancestor is required for virtualized and scrollable items. If there is no ancestor `ScrollView`, the items will always be rendered as though they are within the viewport. | ||
|
|
||
| <center><img src="/docs/assets/virtual_column.svg" width="300" alt="Diagram of VirtualColumn." /></center> | ||
|
|
||
| ### Item Rendering | ||
|
|
||
| `VirtualColumn` expects an immutable `VirtualCollection` of items and an `itemsToKey` function. | ||
|
|
||
| When `VirtualColumn` renders an item, it calls the `children` render function once and memoizes the result. If an item changes, a new item should be provided with the same key. Items with identical keys, as returned by `itemsToKey`, will update the same React element. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto: |
||
|
|
||
| During rendering, `VirtualColumn` wraps each item in a `VirtualView` for virtualization. | ||
|
|
||
| ### Lazy Mounting | ||
|
|
||
| `VirtualColumn` will lazily render list items to fill the viewport and prerender regions of the VirtualView. It will lazily mount items at the bottom of the list. | ||
|
|
||
| ### Composability | ||
|
|
||
| `VirtualColumn` supports nesting of multiple `VirtualColumn` components and other `VirtualCollectionView` lists. | ||
|
|
||
| <center><img src="/docs/assets/virtual_column_nested.svg" width="300" alt="Diagram of two composed VirtualColumns." /></center> | ||
|
|
||
| ## Props | ||
|
|
||
| ### `children` | ||
|
|
||
| | Type | | ||
| | ------ | | ||
| | `md ({item: object, key: string}) => [React Node](react-node)`| | ||
|
|
||
| A render function that maps an item to a React element. The return value is memoized so the function should be deterministic. | ||
|
|
||
| The function is called with the `item` to be mounted and the `key` as returned from `itemToKey`. | ||
|
|
||
| ### `items` | ||
|
|
||
| | Type | | ||
| | -------------------------------------------------- | | ||
| | `md [VirtualCollection](#virtualcollection)<object>` | | ||
|
|
||
| The collection of items to render. Each item should be treated as immutable. If an updated item should be rendered, a new item object should be supplied. | ||
|
|
||
| If you have an array of items and need to create a `VirtualCollection`, you can use `VirtualArray`. | ||
|
|
||
| ### `itemToKey` | ||
|
|
||
| | Type | | ||
| | -------------------------------------------------- | | ||
| | `md (item: object) => string` | | ||
|
|
||
| A function that maps an item to a unique key. If not supplied, the default function returns `item.key`. | ||
|
|
||
| ## Type Definitions | ||
|
|
||
| ### VirtualCollection | ||
|
|
||
| An interface for a collection of items to allow for lazy random access. | ||
|
|
||
| | Type | | ||
| | -------------------------------------------------- | | ||
| | `interface` | | ||
|
|
||
| **Properties:** | ||
| | Name | Type | Description | | ||
| | ------------- | --------------------------------| -------------------- | ||
| | size | `number` | Number of items in collection | | ||
| | at | `(index: number) => object` | Returns the item object at specified `index`| | ||
|
|
||
|
|
||
| ### VirtualArray | ||
|
|
||
| Implements `VirtualCollection` interface for an eagerly initialized array. | ||
|
|
||
|
|
||
| | Type | | ||
| | -------------------------------------------------- | | ||
| | `object` | | ||
|
|
||
| **Properties:** | ||
| | Name | Type | Description | | ||
| | ------------- | ------------------------------------| -------------------- | ||
| | constructor | `(input: Array<object>) => object` | Create an instance of `VirtualArray` with an array | | ||
| | size | `number` | Number of items in collection | | ||
| | at | `(index: number) => object` | Returns the item object at specified `index`| | ||
|
|
||
| ## FAQ | ||
|
|
||
| ### How do we keep props memoized? | ||
|
|
||
| With [React Compiler](https://react.dev/learn/react-compiler/introduction) enabled, props will be memoized for you. Code samples above assume auto-memoization from the compiler. | ||
|
|
||
| If you are not using React Compiler, then you'll want to manually memoize inputs to avoid re-renders. | ||
|
|
||
| ```tsx | ||
| const virtualCollection = useMemo(() => new VirtualArray(items), [items]); | ||
|
|
||
| const itemToKey = useCallback(item => item.key, []); | ||
| const renderItem = useCallback((item, key) => <Text>{item.value}</Text>, []); | ||
|
|
||
| <ScrollView> | ||
| <VirtualColumn items={virtualCollection} itemToKey={itemToKey}> | ||
| {renderItem} | ||
| </VirtualColumn> | ||
| </ScrollView>; | ||
| ``` | ||
| We highly recommend [enabling the compiler](https://react.dev/learn/react-compiler/installation) for your project. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| id: virtualrow | ||
| title: VirtualRow 🧪 | ||
| --- | ||
|
|
||
| import ExperimentalAPIWarning from './\_experimental-api-warning.mdx'; | ||
|
|
||
| <ExperimentalAPIWarning /> | ||
|
|
||
| `VirtualRow` is a row layout component that lazily mounts and virtualizes items. | ||
|
|
||
| ```tsx | ||
| <ScrollView> | ||
| <VirtualRow | ||
| items={new VirtualArray(items)}> | ||
| {(item, key) => <Text>{item.value}</Text>} | ||
| </VirtualRow> | ||
| </ScrollView> | ||
| ``` | ||
|
|
||
| A `ScrollView` ancestor is required for virtualized and scrollable items. If there is no ancestor `ScrollView`, the items will always be rendered as though they are within the viewport. | ||
|
|
||
| <center><img src="/docs/assets/virtual_row.svg" width="500" alt="Diagram of VirtualRow." /></center> | ||
|
|
||
| See [`VirtualColumn`](virtualcolumn) for more details. | ||
|
|
||
| ## Props | ||
|
|
||
| See [`VirtualColumn` props](virtualcolumn#props). |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc sometimes uses both
itemsToKeyanditemToKey, I assume that only one is correct.