useResultSortedRowIds
The useResultSortedRowIds
hook returns the sorted (and optionally, paginated) Ids
of every Row
in the ResultTable
of the given query, and registers a listener so that any changes to those Ids
will cause a re-render.
useResultSortedRowIds(
queryId: string,
cellId?: string,
descending?: boolean,
offset?: number,
limit?: number,
queriesOrQueriesId?: QueriesOrQueriesId,
): Ids
Type | Description | |
---|---|---|
queryId | string | The |
cellId? | string | The |
descending? | boolean | Whether the sorting should be in descending order. |
offset? | number | The number of |
limit? | number | The maximum number of |
queriesOrQueriesId? | QueriesOrQueriesId | The |
returns | Ids | An array of the |
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 useResultSortedRowIds
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 sorted result Row
Ids
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 useResultSortedRowIds
hook by reference. A change to the data in the query re-renders the component.
import {createQueries, createStore} from 'tinybase';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {useResultSortedRowIds} from 'tinybase/ui-react';
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('color');
where('species', 'dog');
},
);
const App = () => (
<span>
{JSON.stringify(
useResultSortedRowIds(
'dogColors',
'color',
false,
0,
undefined,
queries,
),
)}
</span>
);
const app = document.createElement('div');
createRoot(app).render(<App />);
console.log(app.innerHTML);
// -> '<span>["cujo","fido"]</span>'
store.setCell('pets', 'cujo', 'species', 'wolf');
console.log(app.innerHTML);
// -> '<span>["fido"]</span>'
This example creates a Provider context into which a default Queries
object is provided. A component within it then uses the useResultSortedRowIds
hook.
import {Provider, useResultSortedRowIds} from 'tinybase/ui-react';
import {createQueries, createStore} from 'tinybase';
import React from 'react';
import {createRoot} from 'react-dom/client';
const App = ({queries}) => (
<Provider queries={queries}>
<Pane />
</Provider>
);
const Pane = () => (
<span>
{JSON.stringify(useResultSortedRowIds('dogColors', '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('color');
where('species', 'dog');
});
const app = document.createElement('div');
createRoot(app).render(<App queries={queries} />);
console.log(app.innerHTML);
// -> '<span>["cujo","fido"]</span>'
This example creates a Provider context into which a Queries
object is provided, named by Id
. A component within it then uses the useResultSortedRowIds
hook.
import {Provider, useResultSortedRowIds} from 'tinybase/ui-react';
import {createQueries, createStore} from 'tinybase';
import React from 'react';
import {createRoot} from 'react-dom/client';
const App = ({queries}) => (
<Provider queriesById={{petQueries: queries}}>
<Pane />
</Provider>
);
const Pane = () => (
<span>
{JSON.stringify(
useResultSortedRowIds(
'dogColors',
'color',
false,
0,
undefined,
'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('color');
where('species', 'dog');
});
const app = document.createElement('div');
createRoot(app).render(<App queries={queries} />);
console.log(app.innerHTML);
// -> '<span>["cujo","fido"]</span>'
Since
v2.0.0