Skip to content

Collection components

Cimpress UI contains several components that display a collection of items of some kind. Select and Menu are both examples of such components. These item collections can usually be navigated with the keyboard using arrow keys, and many have some form of selection.

A static collection is a collection that does not change over time (it is hard-coded). This is common for components like action menus, where the items are built into the application.

Cimpress UI implements an intuitive JSX-based API for defining collections.

The following example demonstrates how to build a simple static menu:

Sections or groups of items can be constructed by wrapping the items in a section element, as shown in the following example:

A dynamic collection is a collection that is based on data, for example from an API. It may change over time as items are added, updated, or removed from the collection.

Cimpress UI implements a JSX-based interface for dynamic collections, which maps over your data and applies a function for each item to render it.

The following example demonstrates a select component with options based on dynamic data stored in React state:

Favourite fruit

As you can see, the items are passed to the items prop of the top-level component, which iterates over each item and calls the function passed as children to the component. The item object is passed to the function, which then returns a sub-component.

All items in a dynamic collection must have a unique id property. This property is used to determine which items in the collection changed during an update.

You may be wondering why we didn’t use animals.map() in the example above. In fact, this works just fine, but it’s less performant, and you must remember to provide both a React key and an id prop:

Favourite fruit

Using the items prop and providing a render function allows Cimpress UI to automatically cache the results of rendering each item and avoid re-rendering all items in the collection when only one of them changes. This has big performance benefits for large collections.

When you need to add, remove, or change an item, you can do so using a standard React state update.

Sections can be built by returning a section component instead of an item component from the top-level item renderer. Sections also support an items prop and a render function for their children.

Favourite food

When updating nested data, be sure that all parent items change accordingly. Items are immutable, so don’t use mutating methods like push, or replace a property on a parent item.

Virtualization is a performance optimization for large lists. Instead of rendering all items to the DOM, only the visible items are rendered. This reduces memory usage and improves rendering performance.

Virtualization can be enabled by setting the isVirtualized prop to true. Please note that not every collection component supports virtualization. You can verify if a specific component supports virtualization by looking for the isVirtualized prop in its API reference.