TinyBase logoTinyBase

useRelationshipsIds

The useRelationshipsIds hook is used to retrieve the Ids of all the named Relationships objects present in the current Provider component context.

useRelationshipsIds(): Ids
returnsIds

A list of the Ids in the context.

Example

This example adds two named Relationships objects to a Provider context and an inner component accesses their Ids.

import {
  Provider,
  useCreateRelationships,
  useCreateStore,
  useRelationshipsIds,
} from 'tinybase/ui-react';
import {createRelationships, createStore} from 'tinybase';
import React from 'react';
import {createRoot} from 'react-dom/client';

const App = () => {
  const store1 = useCreateStore(createStore);
  const relationships1 = useCreateRelationships(
    store1,
    createRelationships,
  );
  const store2 = useCreateStore(createStore);
  const relationships2 = useCreateRelationships(
    store2,
    createRelationships,
  );
  return (
    <Provider relationshipsById={{relationships1, relationships2}}>
      <Pane />
    </Provider>
  );
};
const Pane = () => <span>{JSON.stringify(useRelationshipsIds())}</span>;

const app = document.createElement('div');
createRoot(app).render(<App />);
console.log(app.innerHTML);
// -> '<span>["relationships1","relationships2"]</span>'

Since

v4.1.0