TinyBase logoTinyBase β

useProvideQueries

The useProvideQueries primitive is used to add a Queries object by Id to a Provider component, but imperatively from a component within it.

useProvideQueries(
  queriesId: string,
  queries: Queries,
): void
TypeDescription
queriesIdstring

The Id of the Queries object to be registered with the Provider.

queriesQueries

The Queries object to be registered.

returnsvoid

This has no return value.

Normally you will register a Queries object by Id in a context by using the queriesById prop of the top-level Provider component. This primitive, however, lets you dynamically add a new Queries object to the context, from within a component. This is useful for applications where the set of Queries objects is not known at the time of the first render of the root Provider.

A Queries object added to the Provider context in this way will be available to other components within the context (using the useQueries primitive and so on). If you use the same Id as an existing Queries object registration, the new one will take priority over one provided by the queriesById prop.

Example

import {createRoot} from 'solid-js';
import {createQueries, createStore} from 'tinybase';
import {useProvideQueries} from 'tinybase/ui-solid';

createRoot((dispose) => {
  const store = createStore().setRow('pets', 'fido', {
    color: 'brown',
    legs: 4,
  });
  const queries = createQueries(store).setQueryDefinition(
    'brownLegs',
    'pets',
    ({select, where}) => {
      select('legs');
      where('color', 'brown');
    },
  );
  useProvideQueries('petQueries', queries);
  console.log(JSON.stringify(queries.getResultTable('brownLegs')));
  // -> '{"fido":{"legs":4}}'
  dispose();
});

Since

v8.3.0