TinyBase logoTinyBase β

getCell

The getCell function returns a reactive object reflecting the value of a Cell in a Row in a Table, and registers a listener so that any changes to that Cell will update current.

getCell(
  tableId: MaybeGetter<string>,
  rowId: MaybeGetter<string>,
  cellId: MaybeGetter<string>,
  storeOrStoreId?: MaybeGetter<undefined | StoreOrStoreId>,
): {current: CellOrUndefined}
TypeDescription
tableIdMaybeGetter<string>

The Id of the Table (or a getter returning it).

rowIdMaybeGetter<string>

The Id of the Row (or a getter returning it).

cellIdMaybeGetter<string>

The Id of the Cell (or a getter returning it).

storeOrStoreId?MaybeGetter<undefined | StoreOrStoreId>

The Store to use (plain value or getter), or its Id.

returns{current: CellOrUndefined}

A reactive object with gettable and settable current.

Since Cells are mutable leaf values in a Store, the returned object's current property can also be assigned to write back to the Store.

Examples

This example uses the getCell function to display a Cell value reactively.

// In a .svelte file:
// const store = createStore().setCell('pets', 'cat', 'name', 'Fido');
// const name = getCell('pets', 'cat', 'name', store);
// $: console.log(name.current); // 'Fido'

This example passes a TinyBase object into a Svelte component and reads the reactive object's current property.

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

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

  let {store} = $props();

  const result = getCell('pets', 'fido', 'species', store);
</script>

{result.current}
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}}));
console.log(app.textContent);
// -> 'dog'

Since

v8.1.0