You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<PageDescription>Many components display a collection of items, and provide functionality such as keyboard navigation, selection, and more. React Spectrum has a consistent, compositional API to define the items displayed in these components.</PageDescription>
12
12
13
+
## Static collections
14
+
15
+
A **static collection** is a collection that does not change over time (e.g. hard coded). This is common for components like menus where the items are built into the application rather than user data.
Sections or groups of items can be constructed by wrapping the items in a section element. A `<Header>` can also be rendered within a section to provide a section title.
A **dynamic collection** is a collection that is based on external data, for example from an API. In addition, it may change over time as items are added, updated, or removed from the collection by a user.
95
+
96
+
Use the `items` prop to provide an array of objects, and a function to render each item as the `children`. This is similar to using `array.map` to render the children, but automatically memoizes the rendering of each item to improve performance.
<Content>For convenience, React Spectrum provides a built-in [useListData](https://react-spectrum.adobe.com/react-stately/useListData.html) hook to manage state for an immutable list of items. It includes methods to add, remove, update, and re-order items, and manage corresponding selection state. See the docs for more details.</Content>
138
+
</InlineAlert>
139
+
140
+
### Unique ids
141
+
142
+
All items in a collection must have a unique id, which is used for [selection](selection.html) and to track item updates. By default, React Spectrum looks for an `id` property on each object in the `items` array. You can also specify an `id` prop when rendering each item. This example uses `item.name` as the `id`.
143
+
144
+
```tsx
145
+
let animals = [
146
+
{name: 'Aardvark'},
147
+
{name: 'Kangaroo'},
148
+
{name: 'Snake'}
149
+
];
150
+
151
+
<Pickerlabel="Animals"items={animals}>
152
+
{item=> (
153
+
/*- begin highlight -*/
154
+
<PickerItemid={item.name}>
155
+
{/*- end highlight -*/}
156
+
{item.name}
157
+
</PickerItem>
158
+
)}
159
+
</Picker>
160
+
```
161
+
13
162
<InlineAlertvariant="notice">
14
-
<Heading>Coming soon</Heading>
15
-
<Content>
16
-
This page will cover collection patterns in Spectrum 2.
17
-
</Content>
163
+
<Heading>React keys</Heading>
164
+
<Content>React Spectrum automatically sets the React `key` using `id`. If using `array.map`, you'll need to set both `key` and `id`.</Content>
18
165
</InlineAlert>
166
+
167
+
### Dependencies
168
+
169
+
Dynamic collections are automatically memoized to improve performance. Rendered item elements are cached based on the object identity of the list item. If rendering an item depends on additional external state, the `dependencies` prop must be provided. This invalidates rendered elements similar to dependencies in React's `useMemo` hook.
Note that adding dependencies will result in the _entire_ list being invalidated when a dependency changes. To avoid this and invalidate only an individual item, update the item object itself rather than accessing external state.
227
+
228
+
### Combining collections
229
+
230
+
To combine multiple sources of data, or mix static and dynamic items, use the `<Collection>` component.
<Content>Unlike React keys which must only be unique within each element, the `id` prop must be globally unique across all sections. In the above example, the ids of `animals` and `people` do not conflict.</Content>
273
+
</InlineAlert>
274
+
275
+
## Asynchronous loading
276
+
277
+
Data can be loaded asynchronously using any data fetching library. [useAsyncList](https://react-spectrum.adobe.com/react-stately/useAsyncList.html) is a built-in option.
278
+
279
+
Many components support infinite scrolling via the `loadingState` and `onLoadMore` props. These trigger loading of additional pages of items automatically as the user scrolls.
0 commit comments