useProvideRelationships
The useProvideRelationships primitive is used to add a Relationships object by Id to a Provider component, but imperatively from a component within it.
useProvideRelationships(
relationshipsId: string,
relationships: Relationships,
): void| Type | Description | |
|---|---|---|
relationshipsId | string | The |
relationships | Relationships | The |
| returns | void | This has no return value. |
Normally you will register a Relationships object by Id in a context by using the relationshipsById prop of the top-level Provider component. This primitive, however, lets you dynamically add a new Relationships object to the context, from within a component. This is useful for applications where the set of Relationships objects is not known at the time of the first render of the root Provider.
A Relationships object added to the Provider context in this way will be available to other components within the context (using the useRelationships primitive and so on). If you use the same Id as an existing Relationships object registration, the new one will take priority over one provided by the relationshipsById prop.
Example
import {createRoot} from 'solid-js';
import {createRelationships, createStore} from 'tinybase';
import {useProvideRelationships} from 'tinybase/ui-solid';
createRoot((dispose) => {
const store = createStore()
.setTable('pets', {fido: {species: 'dog'}})
.setTable('species', {dog: {price: 5}});
const relationships = createRelationships(
store,
).setRelationshipDefinition('petSpecies', 'pets', 'species', 'species');
useProvideRelationships('petRelationships', relationships);
console.log(relationships.getRemoteRowId('petSpecies', 'fido'));
// -> 'dog'
dispose();
});
Since
v8.3.0