React - using Datastore Hooks
Offix Datastore provides react hooks for CRUD operations and subscription.
For a quick start see sample react app.
#
useSaveThe useSave
hook provides a lazy save
function,
a loading indicator loading
and an error
state variable.
The save
function accepts the input and returns a promise of the save result.
#
useQueryYou can query using this hook, with or without filters or id. When no filter or id is supplied all the data is returned.
Query using a filter;
Query by id;
You can also subscribe to changes using the subscribeToUpdates
function.
subscribeToUpdates
can take an array of events to watch
const subscription = subscribeToUpdates([CRUDEvents.ADD, CRUDEvents.UPDATE]);
Offix Datastore reponds to events and updates your Application state for you. You can also override Datastore's event handlers.
#
useLazyQueryThis hook provides a lazy query
function. The query
function accepts
a filter, an id or nothing, in which case, all documents are returned.
The useLazyQuery
hook also provides a subscribeToUpdates
function
that you can use to subscribe to events on your data.
#
useUpdateThe useUpdate
works just like the useSave
hook.
The only difference is that the input to the update
function must have its primary key defined.
The update
function accepts an optional upsert
parameter(which is false
by default) that can be used to
perform a saveOrUpdate
operation instead of an update
operation.
#
useRemove#
useSubscriptionYou can subscribe to multiple events and receive changes data using this hook.
We listen for updates to the task and render them. The data
returned
by useSubscription
is the data carried by the change event.
We can listen to all events on all documents
const { data } = useSubscription(TaskModel);