TinyBase logoTinyBase β

useProvideIndexes

The useProvideIndexes primitive is used to add an Indexes object by Id to a Provider component, but imperatively from a component within it.

useProvideIndexes(
  indexesId: string,
  indexes: Indexes,
): void
TypeDescription
indexesIdstring

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

indexesIndexes

The Indexes object to be registered.

returnsvoid

This has no return value.

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

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

Example

import {createRoot} from 'solid-js';
import {createIndexes, createStore} from 'tinybase';
import {useProvideIndexes} from 'tinybase/ui-solid';

createRoot((dispose) => {
  const store = createStore().setCell('pets', 'fido', 'color', 'brown');
  const indexes = createIndexes(store).setIndexDefinition(
    'petsByColor',
    'pets',
    'color',
  );
  useProvideIndexes('petIndexes', indexes);
  console.log(JSON.stringify(indexes.getSliceIds('petsByColor')));
  // -> '["brown"]'
  dispose();
});

Since

v8.3.0