TinyBase logoTinyBase

useGoToCallback

The useGoToCallback hook returns a parameterized callback that can be used to move the state of the underlying Store backwards or forwards to a specified checkpoint.

useGoToCallback<Parameter>(
  getCheckpointId: (parameter: Parameter) => string,
  getCheckpointIdDeps?: DependencyList,
  checkpointsOrCheckpointsId?: CheckpointsOrCheckpointsId,
  then?: (checkpoints: Checkpoints, checkpointId: string) => void,
  thenDeps?: DependencyList,
): ParameterizedCallback<Parameter>
TypeDescription
getCheckpointId(parameter: Parameter) => string

A function which returns an Id that will be used to indicate which checkpoint to move to, based on the parameter the callback will receive (and which is most likely a DOM event).

getCheckpointIdDeps?DependencyList

An optional array of dependencies for the getCheckpointId function, which, if any change, result in the regeneration of the callback. This parameter defaults to an empty array.

checkpointsOrCheckpointsId?CheckpointsOrCheckpointsId

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

then?(checkpoints: Checkpoints, checkpointId: string) => void

A function which is called after the checkpoint is moved, with a reference to the Checkpoints object and the checkpoint Id moved to.

thenDeps?DependencyList

An optional array of dependencies for the then function, which, if any change, result in the regeneration of the callback.

returnsParameterizedCallback<Parameter>

A parameterized callback for subsequent use. This parameter defaults to an empty array.

This hook is useful, for example, when creating an event handler that will move the checkpoint. In this case, the parameter will likely be the event, so that you can use data from it as the checkpoint Id to move to.

The optional first parameter is a function which will produce the label that will then be used to name the checkpoint.

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 second 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 checkpoint has been set.

The Checkpoints object for which the callback will set the checkpoint (indicated by the hook's checkpointsOrCheckpointsId parameter) is always automatically used as a hook dependency for the callback.

Example

This example uses the useGoToCallback hook to create an event handler which moves to a checkpoint when the span element is clicked.

import {createCheckpoints, createStore} from 'tinybase';
import React from 'react';
import {createRoot} from 'react-dom/client';
import {useGoToCallback} from 'tinybase/ui-react';

const store = createStore().setTables({pets: {nemo: {species: 'fish'}}});
const checkpoints = createCheckpoints(store);
const App = () => {
  const handleClick = useGoToCallback(() => '0', [], checkpoints);
  return (
    <span id="span" onClick={handleClick}>
      Goto 0
    </span>
  );
};

const app = document.createElement('div');
createRoot(app).render(<App />);
const _span = app.querySelector('span');

store.setCell('pets', 'nemo', 'color', 'orange');
checkpoints.addCheckpoint();
console.log(checkpoints.getCheckpointIds());
// -> [["0"], "1", []]

// User clicks the <span> element:
// -> _span MouseEvent('click', {bubbles: true})
console.log(checkpoints.getCheckpointIds());
// -> [[], "0", ["1"]]

Since

v1.0.0