TinyBase logoTinyBase β

useCreateStore

Essential

The useCreateStore primitive is used to create a Store within a Solid application with convenient ownership.

useCreateStore(create: () => Store): Accessor<Store>
TypeDescription
create() => Store

A function for performing the creation of the Store, plus any additional steps such as adding data or listeners, and returning it.

returnsAccessor<Store>

A reference to the Store.

It is possible to create a Store outside of the Solid app with the regular createStore function and pass it in, but you may prefer to create it within the app, perhaps inside the top-level component. The Store is created once in the current reactive owner and is disposed with that owner.

In Solid, changing values should be read inside the create function via signals or other Accessors, rather than via a dependency list.

Example

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

createRoot((dispose) => {
  const store = useCreateStore(() =>
    createStore().setCell('pets', 'fido', 'species', 'dog'),
  );
  console.log(store().getCell('pets', 'fido', 'species'));
  // -> 'dog'
  dispose();
});

Since

v8.3.0