useSetParamValueCallback
The useSetParamValueCallback hook returns a parameterized callback that can be used to set a single parameter value for a query.
useSetParamValueCallback<Parameter>(
queryId: string | GetId<Parameter>,
paramId: string | GetId<Parameter>,
getParamValue: (parameter: Parameter, queries: Queries) => ParamValue,
getParamValueDeps?: DependencyList,
queriesOrQueriesId?: QueriesOrQueriesId,
then?: (queries: Queries, paramValue: ParamValue) => void,
thenDeps?: DependencyList,
): ParameterizedCallback<Parameter>| Type | Description | |
|---|---|---|
queryId | string | GetId<Parameter> | The |
paramId | string | GetId<Parameter> | The |
getParamValue | (parameter: Parameter, queries: Queries) => ParamValue | A function which returns the parameter value that will be used to update the query, based on the parameter the callback will receive (and which is most likely a DOM event). |
getParamValueDeps? | DependencyList | An optional array of dependencies for the |
queriesOrQueriesId? | QueriesOrQueriesId | The |
then? | (queries: Queries, paramValue: ParamValue) => void | A function which is called after the mutation, with a reference to the |
thenDeps? | DependencyList | An optional array of dependencies for the |
| returns | ParameterizedCallback<Parameter> | A parameterized callback for subsequent use. |
This hook is useful, for example, when creating an event handler that will update query parameters based on user interaction. In this case, the parameter will likely be the event, so that you can use data from it to update the query parameter.
The third parameter is a function which will produce the parameter value that will then be used to update the query in the callback.
If that function has any other dependencies, the changing of which should also cause the callback to be recreated, you can provide them in an array in the optional fourth parameter, just as you would for any React hook with dependencies.
For convenience, you can optionally provide a then function (with its own set of dependencies) which will be called just after the query parameter has been updated.
The Queries object to which the callback will make the mutation (indicated by the hook's queriesOrQueriesId parameter) is always automatically used as a hook dependency for the callback.
Example
This example uses the useSetParamValueCallback hook to create an event handler which updates a query parameter when an input element changes.
import React from 'react';
import {createRoot} from 'react-dom/client';
import {createQueries, createStore} from 'tinybase';
import {useResultTable, useSetParamValueCallback} 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);
queries.setQueryDefinition(
'dogOrCat',
'pets',
({select, where, param}) => {
select('species');
where('species', param('species'));
},
{species: 'dog'},
);
const App = () => {
const handleInput = useSetParamValueCallback(
'dogOrCat',
'species',
(e) => e.target.value,
[],
queries,
(_queries, paramValue) => console.log(`Updated: ${paramValue}`),
);
return (
<div>
<input id="input" onInput={handleInput} />
{JSON.stringify(useResultTable('dogOrCat', queries))}
</div>
);
};
const app = document.createElement('div');
createRoot(app).render(<App />);
const input = app.querySelector('input');
console.log(app.innerHTML);
// ->
`
<div>
<input id="input">
{"fido":{"species":"dog"},"cujo":{"species":"dog"}}
</div>
`;
// User types 'cat' in the input and event fires:
input.value = 'cat';
// -> input Event('input', {bubbles: true})
// -> 'Updated: cat'
console.log(app.innerHTML);
// ->
`
<div>
<input id="input">
{"felix":{"species":"cat"}}
</div>
`;
Since
v7.2.0