TinyBase

useResultCell

The useResultCell hook returns the value of a single Cell in a given Row in the result Table of the given query, and registers a listener so that any changes to that value will cause a re-render.

useResultCell(
  queryId: string,
  rowId: string,
  cellId: string,
  queriesOrQueriesId?: QueriesOrQueriesId,
): Cell | undefined
TypeDescription
queryIdstring

The Id of the query.

rowIdstring

The Id of the Row in the result Table.

cellIdstring

The Id of the Cell in the Row.

queriesOrQueriesId?QueriesOrQueriesId

The Queries object to be accessed: omit for the default context Queries object, provide an Id for a named context Queries object, or provide an explicit reference.

returnsCell | undefined

The value of the Cell, or `undefined`.

A Provider component is used to wrap part of an application in a context, and it can contain a default Queries object or a set of Queries objects named by Id. The useResultCell hook lets you indicate which Queries object to get data for: omit the final optional final parameter for the default context Queries object, provide an Id for a named context Queries object, or provide a Queries object explicitly by reference.

When first rendered, this hook will create a listener so that changes to the result Cell will cause a re-render. When the component containing this hook is unmounted, the listener will be automatically removed.

Examples

This example creates a Queries object outside the application, which is used in the useResultCell hook by reference. A change to the data in the query re-renders the component.

const store = createStore().setTable('pets', {
  fido: {species: 'dog', color: 'brown'},
  felix: {species: 'cat', color: 'black'},
  cujo: {species: 'dog', color: 'black'},
});
const queries = createQueries(store).setQueryDefinition(
  'dogColors',
  'pets',
  ({select, where}) => {
    select('species');
    select('color');
    select('legs');
    where('species', 'dog');
  },
);
const App = () => (
  <span>{useResultCell('dogColors', 'fido', 'color', queries)}</span>
);

const app = document.createElement('div');
ReactDOM.render(<App />, app);
console.log(app.innerHTML);
// -> '<span>brown</span>'

store.setCell('pets', 'fido', 'color', 'walnut');
console.log(app.innerHTML);
// -> '<span>walnut</span>'

This example creates a Provider context into which a default Queries object is provided. A component within it then uses the useResultCell hook.

const App = ({queries}) => (
  <Provider queries={queries}>
    <Pane />
  </Provider>
);
const Pane = () => (
  <span>{useResultCell('dogColors', 'fido', 'color')}</span>
);

const queries = createQueries(
  createStore().setTable('pets', {
    fido: {species: 'dog', color: 'brown'},
    felix: {species: 'cat', color: 'black'},
    cujo: {species: 'dog', color: 'black'},
  }),
).setQueryDefinition('dogColors', 'pets', ({select, where}) => {
  select('species');
  select('color');
  where('species', 'dog');
});
const app = document.createElement('div');
ReactDOM.render(<App queries={queries} />, app);
console.log(app.innerHTML);
// -> '<span>brown</span>'

This example creates a Provider context into which a Queries object is provided, named by Id. A component within it then uses the useResultCell hook.

const App = ({queries}) => (
  <Provider queriesById={{petQueries: queries}}>
    <Pane />
  </Provider>
);
const Pane = () => (
  <span>{useResultCell('dogColors', 'fido', 'color', 'petQueries')}</span>
);

const queries = createQueries(
  createStore().setTable('pets', {
    fido: {species: 'dog', color: 'brown'},
    felix: {species: 'cat', color: 'black'},
    cujo: {species: 'dog', color: 'black'},
  }),
).setQueryDefinition('dogColors', 'pets', ({select, where}) => {
  select('species');
  select('color');
  where('species', 'dog');
});
const app = document.createElement('div');
ReactDOM.render(<App queries={queries} />, app);
console.log(app.innerHTML);
// -> '<span>brown</span>'

Since

v2.0.0