TinyBase logoTinyBase β

onTable

The onTable function registers a listener that is called whenever data in a specified Table changes.

onTable(
  tableId: MaybeGetter<IdOrNull>,
  listener: TableListener,
  mutator?: boolean,
  storeOrStoreId?: MaybeGetter<undefined | StoreOrStoreId>,
): void
TypeDescription
tableIdMaybeGetter<IdOrNull>

The Id of the Table to listen to, or null to listen to any Table.

listenerTableListener

The function to call when the Table changes.

mutator?boolean

An optional boolean indicating the listener mutates Store data.

storeOrStoreId?MaybeGetter<undefined | StoreOrStoreId>

The Store to use, or its Id.

returnsvoid

This has no return value.

Example

This example registers a Svelte listener and responds to a TinyBase change.

App.svelte
<svelte:options runes={true} />

<script>
  import {onTable} from 'tinybase/ui-svelte';

  let {store} = $props();
  let seen = $state('');

  onTable('pets', () => (seen = 'changed'), false, store);
</script>

{seen}
import {flushSync, mount} from 'svelte';
import {createStore} from 'tinybase';
import App from './App.svelte';

const store = createStore()
  .setTables({
    pets: {
      fido: {species: 'dog', color: 'brown', sold: false, next: 'felix'},
      felix: {species: 'cat', color: 'black', sold: true},
    },
    species: {dog: {price: 5}, cat: {price: 4}},
  })
  .setValues({open: true, employees: 3});
const app = document.body.appendChild(document.createElement('div'));
flushSync(() => mount(App, {target: app, props: {store}}));
flushSync(() => {
  store.setCell('pets', 'fido', 'color', 'white');
});
flushSync();
console.log(app.textContent);
// -> 'changed'

Since

v8.1.0