Working with Data
When working with data, you will mostly interact with the
<DataSource /> component, which is responsible for handling and managing the data and passing it down to the <InfiniteTable /> component, which is the rendering engine for the DataGrid.So we provide those two components (as named exports) inside
@infinite-table/infinite-react package:<DataSource />- our data-handling component<InfiniteTable />- our virtualized component
The
<DataSource/> component is responsible for the data the management layer.Probably the most important prop for the
<DataSource /> component is the primaryKey prop. It specifies the property of the data object that is used as a unique identifier for data rows/items.The
<DataSource /> is a generic React TypeScript component that can be bound to an array of items of the generic type.In this documentation, we'll use
DATA_TYPE when referring to the generic type. Rarely, we'll use T.Most of our examples in these docs have a
Developer or Employee TypeScript data type used as the generic type for the <DataSource /> component.In the snippet above, we see 3 important details:
- the component is bound to the
Employeetype - we use a
primaryKeyproperty - here it isid, but since the bound type isEmployee,primaryKeyiskeyof Employee - we pass the
employeesarray as thedataproperty.
The
data prop can be either:- an array of the bound generic type - here
Employee[] - a Promise tha resolves to an array like the above
- a function that returns any of the above
#
Data loading example with promise
loading demo…
Data Loading Strategies#
We're aware there are countless strategies for loading data - each with its own strengths. We decided we should focus on building what we do best, namely building virtualized components, so we encourage you to use your preferred data-fetching library/solution. This being said, we still provide you with the flexibility you need when using the
<DataSource/>, so here's what you can use for the data prop of the component:- an array of the bound type
- a Promise that resolves to an array of the bound type
- a function that returns any of the above
While you're loading the data, you can always render a loading indicator - pass the
loading prop into the component (along with loadingText prop in the <InfiniteTable /> component if you want to customize the message).Using fetch#
For basic datasets, which have simple data requirements, using
fetch is probably sufficient, so here is an example:#
Using fetch for remote data
loading demo…
Re-fetching on change
It's important to note you can re-fetch data by changing the reference you pass as the
data prop to the <DataSource/> component.Passing another
data function, will cause the component to re-execute the function and thus load new data.Alternatively, you can use the
refetchKey prop to trigger a re-fetch - give it a new value (eg: use it as a counter, and increment it) and the component will re-fetch the data.#
Re-fetching data
loading demo…
Live Updates#
You can update your data in real-time by using our DataSource API.
DataSource API
Read more about how to use our API to update your data in real-time