useProvideMetrics
The useProvideMetrics primitive is used to add a Metrics object by Id to a Provider component, but imperatively from a component within it.
useProvideMetrics(
metricsId: string,
metrics: Metrics,
): void| Type | Description | |
|---|---|---|
metricsId | string | The |
metrics | Metrics | The |
| returns | void | This has no return value. |
Normally you will register a Metrics object by Id in a context by using the metricsById prop of the top-level Provider component. This primitive, however, lets you dynamically add a new Metrics object to the context, from within a descendent component. This is useful for applications where the set of Metrics objects is not known at the time of the first render of the root Provider.
A Metrics object added to the Provider context in this way will be available to other components within the context (using the useMetrics primitive and so on). If you use the same Id as an existing Metrics object registration, the new one will take priority over one provided by the metricsById prop.
Example
import {createRoot} from 'solid-js';
import {createMetrics, createStore} from 'tinybase';
import {useProvideMetrics} from 'tinybase/ui-solid';
createRoot((dispose) => {
const store = createStore().setCell('pets', 'fido', 'color', 'brown');
const metrics = createMetrics(store).setMetricDefinition(
'petCount',
'pets',
'count',
);
useProvideMetrics('petMetrics', metrics);
console.log(metrics.getMetric('petCount'));
// -> 1
dispose();
});
Since
v8.3.0