Updating Data in Real-Time
Real-Time updates of data are possible via the DataSource API.
In this page we explain some of the complexities and features involved.
Getting a reference to the DataSource API#
Data Updates are related to the
DataSource component, therefore make sure you use the DataSource API for this.You can get a reference to the DataSource API
- either by using the DataSource onReady prop
- or by using the InfiniteTable onReady prop.
Updating Rows#
To update the data of a row, you need to know the
primaryKey for that row and use the updateData method of the DataSource API.dataSourceApi.updateData({
// if the primaryKey is the "id" field, make sure to include it
id: 1,
// and then include any properties you want to update - in this case, the name and age
name: 'Bob Blue',
age: 35,
});To update multiple rows, you need to pass the array of data items to the
updateDataArray method.dataSourceApi.updateDataArray([
{
id: 1, // if the primaryKey is the "id" field, make sure to include it
name: 'Bob Blue',
age: 35,
},
{
id: 2, // primaryKey for this row
name: 'Alice Green',
age: 25,
},
]);#
Live data updates with DataSourceApi.updateData
The DataSource has 10k items - use the Start/Stop button to see updates in real-time.
In this example, we're updating 5 rows (in the visible viewport) every 30ms.
The update rate could be much higher, but we're keeping it at current levels to make it easier to see the changes.
loading demo…
For updating multiple rows, use the
updateDataArray method.When updating a row, the data object you pass to the
updateData method needs to at least include the primaryKey field. Besides that field, it can include any number of properties you want to update for the specific row.Batching updates#
All the methods for updating/inserting/deleting rows exposed via the DataSource API are batched by default. So you can call multiple methods on the same raf (requestAnimationFrame), and they will trigger a single render.
All the function calls made in the same raf return the same promise, which is resolved when the data is persisted to the
DataSourceconst promise1 = dataSourceApi.updateData({
id: 1,
name: 'Bob Blue',
});
const promise2 = dataSourceApi.updateDataArray([
{ id: 2, name: 'Alice Green' },
{ id: 3, name: 'John Red' },
]);
promise1 === promise2; // trueInserting Rows#
To insert a new row into the
DataSource, you need to use the insertData method. For inserting multiple rows at once, use the insertDataArray method.dataSourceApi.insertData(
{
id: 10,
name: 'Bob Blue',
age: 35,
salary: 12_000,
stack: 'frontend',
//...
},
{
position: 'before',
primaryKey: 2,
},
);When you insert new data, as a second parameter, you have to provide an object that specifies the insert
position.Valid values for the insert
position are:start|end- inserts the data at the beginning or end of the data source. In this case, noprimaryKeyis needed.
before|after- inserts the data before or after the data item that has the specified primary key. In thise case, theprimaryKeyis required.
#
Using dataSourceApi.insertData
Click any row in the table to make it the current active row, and then use the second button to add a new row after the active row.
loading demo…
Adding rows#
In addition to the
insertData and insertDataArray methods, the DataSource also exposes the addData and addDataArray methods (same as insert with position=end).Deleting Rows#
To delete rows from the
DataSource you either need to know the primaryKey for the row you want to delete, or you can pass the data object (or at least a partial that contains the primaryKey) for the row you want to delete.All the following methods are available via the DataSource API: