react-batch-tracker
v0.0.9
Published
Keep track of state manipulating events and batch them into a single callback function.
Downloads
6
Maintainers
Readme
react-batch-tracker
this is a react context based npm package for batching calls to a function. it supports creating several trackers that can track state change by following individual data objects. the goal with the lib is to facilitate autosave and undo functionality, and for optimising client -> API requests with callbacks containing a batched data set.
getting started
installation
npm install
yarn install
usage
first wrap your app with the provider
then get access to the api in a component by using the useBatchTracker
function
create a batch tracker
before anything doing else, you need to create a new batch tracker. you need to give it a name, a timout (in ms) and a callback function.
since this lib is based on react context, you will be able to get access to all the initiated trackers globally in your app.
it makes sense to create a new tracker for each component or group of component whose state has some common goal, either it being that they have a common state that needs to be CRUD:ed with a database with an autosave functionality. or maybe it makes sense from a UX perspective to keep track of their combined history of states to implement something like an undo/redo functionality.
register actions
whenever an event occurs that should trigger the countdown for the callback function, you need it to register an action. it makes sense to have this happen on an onChange
type of event, as shown below.
specific use cases
auto save
the code above will produce a behaviour that will be shown in the follow gif
auto save with undo functionality
tbc