TinyBase logoTinyBase β

onHasRow

The onHasRow function registers a listener that is called whenever a specified Row is added to or removed from a Table.

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

listenerHasRowListener

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 {onHasRow} from 'tinybase/ui-svelte';

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

  onHasRow('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.delRow('pets', 'fido');
});
flushSync();
console.log(app.textContent);
// -> 'changed'

Since

v8.1.0