Using Solid Primitives
The primitives in the ui-solid module return Solid Accessor functions for reactive TinyBase data. Calling the Accessor reads the current value, and any Solid computation that calls it will update when the underlying Store data changes.
Here is the useCell primitive reading the color of a pet:
import {createRoot} from 'solid-js';
import {createStore} from 'tinybase';
import {useCell, useSetCellCallback, useTable} from 'tinybase/ui-solid';
const store = createStore().setCell('pets', 'fido', 'color', 'brown');
createRoot((dispose) => {
const color = useCell('pets', 'fido', 'color', store);
console.log(color());
// -> 'brown'
store.setCell('pets', 'fido', 'color', 'walnut');
console.log(color());
// -> 'walnut'
dispose();
});
There are primitives that correspond to each of the Store getter methods:
- The
useValuesprimitive is the reactive equivalent of thegetValuesmethod. - The
useValueIdsprimitive is the reactive equivalent of thegetValueIdsmethod. - The
useValueprimitive is the reactive equivalent of thegetValuemethod.
And for tabular data:
- The
useTablesprimitive is the reactive equivalent of thegetTablesmethod. - The
useTableIdsprimitive is the reactive equivalent of thegetTableIdsmethod. - The
useTableprimitive is the reactive equivalent of thegetTablemethod. - The
useTableCellIdsprimitive is the reactive equivalent of thegetTableCellIdsmethod. - The
useRowIdsprimitive is the reactive equivalent of thegetRowIdsmethod. - The
useSortedRowIdsprimitive is the reactive equivalent of thegetSortedRowIdsmethod. - The
useRowprimitive is the reactive equivalent of thegetRowmethod. - The
useCellIdsprimitive is the reactive equivalent of thegetCellIdsmethod. - The
useCellprimitive is the reactive equivalent of thegetCellmethod.
They have the same value types, wrapped in Accessors. For example, the useTable primitive returns an Accessor for a Table object:
createRoot((dispose) => {
const table = useTable('pets', store);
console.log(JSON.stringify(table()));
// -> '{"fido":{"color":"walnut"}}'
store.setCell('pets', 'fido', 'species', 'dog');
console.log(JSON.stringify(table()));
// -> '{"fido":{"color":"walnut","species":"dog"}}'
dispose();
});
When the owner that created a primitive is disposed, the listener registered with TinyBase is removed automatically. This keeps Store subscriptions aligned with Solid's lifecycle.
Using Primitives To Set Data
In an interactive application, you don't just want to read data. You also want to be able to set it in response to user's actions. For this purpose, there is a group of primitives that return callbacks for setting data based on events.
This example uses the useSetCellCallback primitive to update a Cell:
createRoot((dispose) => {
const sold = useCell('pets', 'fido', 'sold', store);
const sell = useSetCellCallback('pets', 'fido', 'sold', () => true, store);
sell();
console.log(sold());
// -> true
dispose();
});
Reactive Parameters With MaybeAccessor
Many ui-solid primitives accept either a plain value or a Solid Accessor function. This is the MaybeAccessor type.
Passing an Accessor is the idiomatic way to connect primitive arguments to changing props or signals. For example, a component can pass () => props.rowId as the Row Id so the primitive re-reads the Store when the prop changes.
Summary
The primitives in the ui-solid module make it easy to connect Solid reactivity to TinyBase Store data, while callback and listener primitives let you handle mutations and side-effects. Next, let's look at the view components in the Using Solid Components guide.