TinyBase logoTinyBase β

useTableState

The useTableState primitive returns a Table and a function to set it, following the same pattern as Solid's createSignal convention.

useTableState(
  tableId: MaybeAccessor<string>,
  storeOrStoreId?: MaybeAccessor<undefined | StoreOrStoreId>,
): [Accessor<Table>, (table: Table) => void]
TypeDescription
tableIdMaybeAccessor<string>

The Id of the Table in the Store.

storeOrStoreId?MaybeAccessor<undefined | StoreOrStoreId>

The Store to be accessed: omit for the default context Store, provide an Id for a named context Store, or provide an explicit reference.

returns[Accessor<Table>, (table: Table) => void]

An array containing the Table and a function to set it.

This is a convenience primitive that combines the useTable and useSetTableCallback primitives. It's useful when you need both read and write access to a Table in a single component.

A Provider component is used to wrap part of an application in a context, and it can contain a default Store or a set of Store objects named by Id. The useTableState primitive lets you indicate which Store to use: omit the final parameter for the default context Store, provide an Id for a named context Store, or provide a Store explicitly by reference.

Example

This example creates a Store, binds it to the primitive, and reads the resulting Solid Accessor.

import {createRoot} from 'solid-js';
import {createStore} from 'tinybase';
import {useTableState} from 'tinybase/ui-solid';

createRoot((dispose) => {
  const store = createStore();
  const [table, setTable] = useTableState('pets', store);
  setTable({fido: {species: 'dog'}});
  console.log(JSON.stringify(table()));
  // -> '{"fido":{"species":"dog"}}'
  dispose();
});

Since

v8.3.0