TinyBase logoTinyBase β

onRow

The onRow function registers a listener that is called whenever data in a specified Row changes.

onRow(
  tableId: MaybeGetter<IdOrNull>,
  rowId: MaybeGetter<IdOrNull>,
  listener: RowListener,
  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.

rowIdMaybeGetter<IdOrNull>

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

listenerRowListener

The function to call when the Row 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 {onRow} from 'tinybase/ui-svelte';

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

  onRow('pets', 'fido', () => (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