TinyBase logoTinyBase β

useCreateQueries

The useCreateQueries hook is used to create a Queries object within a React application with convenient memoization.

useCreateQueries(
  store: undefined | Store,
  create: (store: Store) => Queries,
  createDeps?: DependencyList,
): Queries | undefined
TypeDescription
storeundefined | Store

A reference to the Store for which to create a new Queries object.

create(store: Store) => Queries

An optional callback for performing post-creation steps on the Queries object, such as adding definitions or listeners.

createDeps?DependencyList

An optional array of dependencies for the create function, which, if any change, result in its rerun. This parameter defaults to an empty array.

returnsQueries | undefined

A reference to the Queries object.

It is possible to create a Queries object outside of the React app with the regular createQueries function and pass it in, but you may prefer to create it within the app, perhaps inside the top-level component. To prevent a new Queries object being created every time the app renders or re-renders, since v5.0 this hook performs the creation in an effect. As a result it will return undefined on the brief first render (or if the Store is not yet defined), which you should defend against.

If your create function contains other dependencies, the changing of which should also cause the Queries object to be recreated, you can provide them in an array in the optional second parameter, just as you would for any React hook with dependencies.

This hook ensures the Queries object is destroyed whenever a new one is created or the component is unmounted.

Examples

This example creates a Queries object at the top level of a React application. Even though the App component is rendered twice, the Queries object creation only occurs once by default.

import {createQueries, createStore} from 'tinybase';
import {useCreateQueries, useCreateStore} from 'tinybase/ui-react';
import React from 'react';
import {createRoot} from 'react-dom/client';

const App = () => {
  const store = useCreateStore(() =>
    createStore().setTable('pets', {
      fido: {species: 'dog', color: 'brown'},
      felix: {species: 'cat', color: 'black'},
      cujo: {species: 'dog', color: 'black'},
    }),
  );
  const queries = useCreateQueries(store, (store) => {
    console.log('Queries created');
    return createQueries(store).setQueryDefinition(
      'dogColors',
      'pets',
      ({select, where}) => {
        select('color');
        where('species', 'dog');
      },
    );
  });
  return (
    <span>{queries?.getResultCell('dogColors', 'fido', 'color')}</span>
  );
};

const app = document.createElement('div');
const root = createRoot(app);
root.render(<App />);
// -> 'Queries created'

root.render(<App />);
// No second Queries creation

console.log(app.innerHTML);
// -> '<span>brown</span>'

This example creates a Queries object at the top level of a React application. The App component is rendered twice, each with a different top-level prop. The useCreateQueries hook takes the resultCell prop as a dependency, and so the Queries object is created again on the second render.

import {createQueries, createStore} from 'tinybase';
import {useCreateQueries, useCreateStore} from 'tinybase/ui-react';
import React from 'react';
import {createRoot} from 'react-dom/client';

const App = () => {
  const store = useCreateStore(() =>
    createStore().setTable('pets', {
      fido: {species: 'dog', color: 'brown'},
      felix: {species: 'cat', color: 'black'},
      cujo: {species: 'dog', color: 'black'},
    }),
  );
  const queries = useCreateQueries(store, (store) => {
    console.log('Queries created');
    return createQueries(store).setQueryDefinition(
      'dogColors',
      'pets',
      ({select, where}) => {
        select('color');
        where('species', 'dog');
      },
    );
  });
  return (
    <span>{queries?.getResultCell('dogColors', 'fido', 'color')}</span>
  );
};

const app = document.createElement('div');
const root = createRoot(app);
root.render(<App />);
// -> 'Queries created'

root.render(<App />);
// No second Queries creation

console.log(app.innerHTML);
// -> '<span>brown</span>'

Since

v2.0.0