TinyBase logoTinyBase β

hasValue

The hasValue function returns a reactive object indicating whether a Value exists in the Store, and registers a listener so that any changes to that result will update current.

hasValue(
  valueId: MaybeGetter<string>,
  storeOrStoreId?: MaybeGetter<undefined | StoreOrStoreId>,
): {current: boolean}
TypeDescription
valueIdMaybeGetter<string>

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

storeOrStoreId?MaybeGetter<undefined | StoreOrStoreId>

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

returns{current: boolean}

A reactive object with a current boolean property.

Example

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 {hasValue} from 'tinybase/ui-svelte';

  let {store} = $props();

  const result = hasValue('open', 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);
// -> 'true'

Since

v8.1.0