TinyBase logoTinyBase β

provideStore

The provideStore function registers a Store with a given Id into the current Provider context, making it available to all descendant components.

provideStore(
  storeId: string,
  store: Store,
): void
TypeDescription
storeIdstring

The Id to register the Store under.

storeStore

The Store to register.

returnsvoid

This has no return value.

The provideStore function must be called inside a Svelte component's <script> block that is a descendant of a Provider component.

Example

This example registers a TinyBase object dynamically in a Provider context.

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

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

  let {store} = $props();

  provideStore('registered', store);
</script>
Reader.svelte
<svelte:options runes={true} />

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

  const ids = getStoreIds();
</script>

{JSON.stringify(getStoreIds().current)}
App.svelte
<svelte:options runes={true} />

<script>
  import {Provider} from 'tinybase/ui-svelte';
  import Reader from './Reader.svelte';
  import Registrar from './Registrar.svelte';

  let {store} = $props();
</script>

<Provider>
  <Registrar {store} />
  <Reader />
</Provider>
import {flushSync, mount} from 'svelte';
import {createStore} from 'tinybase';
import App from './App.svelte';

const store = createStore().setCell('pets', 'fido', 'species', 'dog');
const app = document.body.appendChild(document.createElement('div'));
flushSync(() => mount(App, {target: app, props: {store}}));
flushSync();
console.log(app.textContent);
// -> ' ["registered"]'

Since

v8.1.0