useSetValueCallback
The useSetValueCallback
hook returns a parameterized callback that can be used to set the data of a single Value
in a Store
.
useSetValueCallback<Parameter>(
valueId: string | GetId<Parameter>,
getValue: (parameter: Parameter, store: Store) => Value | MapValue,
getValueDeps?: DependencyList,
storeOrStoreId?: StoreOrStoreId,
then?: (store: Store, value: Value | MapValue) => void,
thenDeps?: DependencyList,
): ParameterizedCallback<Parameter>
Type | Description | |
---|---|---|
valueId | string | GetId<Parameter> | The |
getValue | (parameter: Parameter, store: Store) => Value | MapValue | A function which returns the |
getValueDeps? | DependencyList | An optional array of dependencies for the |
storeOrStoreId? | StoreOrStoreId | The |
then? | (store: Store, value: Value | MapValue) => 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 mutate the data in the Store
. In this case, the parameter will likely be the event, so that you can use data from it as part of the mutation.
The second parameter is a function which will produce the Value
object that will then be used to update the Store
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 third 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 Store
has been updated. This is a useful place to call the addCheckpoint
method, for example, if you wish to add the mutation to your application's undo stack.
The Store
to which the callback will make the mutation (indicated by the hook's storeOrStoreId
parameter) is always automatically used as a hook dependency for the callback.
Example
This example uses the useSetValueCallback
hook to create an event handler which updates the Store
when the span
element is clicked.
import {useSetValueCallback, useValues} from 'tinybase/ui-react';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {createStore} from 'tinybase';
const store = createStore().setValue('open', true);
const App = () => {
const handleClick = useSetValueCallback(
'bubbles',
(e) => e.bubbles,
[],
store,
(store, value) => console.log(`Updated: ${JSON.stringify(value)}`),
);
return (
<span id="span" onClick={handleClick}>
{JSON.stringify(useValues(store))}
</span>
);
};
const app = document.createElement('div');
createRoot(app).render(<App />);
const span = app.querySelector('span');
console.log(span.innerHTML);
// -> '{"open":true}'
// User clicks the <span> element:
// -> span MouseEvent('click', {bubbles: true})
// -> 'Updated: true'
console.log(span.innerHTML);
// -> '{"open":true,"bubbles":true}'
Since
v3.0.0