SDK (React)
Use the bknd SDK for React
For all SDK options targeting React, you always have 2 options:
- use simple hooks which are solely based on the API
- use the query hook that makes wraps the API in SWR
To use the simple hook that returns the Api, you can use:
useApiQuery([selector], [options])
This hook wraps the API class in an SWR hook for convenience. You can use any API endpoint supported, like so:
Props
-
selector: (api: Api) => FetchPromise
The first parameter is a selector function that provides an Api instance and expects an endpoint function to be returned.
-
options
: optional object that inherits fromSWRConfiguration
enabled
: Determines whether this hook should trigger a fetch of the data or not.refine
: Optional refinement that is called after a response from the API has been received. Useful to omit irrelevant data from the response (see example below).
Using mutations
To query and mutate data using this hook, you can leverage the parameters returned. In the
following example we’ll also use a refine
function as well as revalidateOnFocus
(option from
SWRConfiguration
) so that our data keeps updating on window focus change.
useEntity()
This hook wraps the endpoints of DataApi
and returns CRUD options as parameters:
If you only supply the entity name as string without an ID, the read
method will fetch a list
of entities instead of a single entry.
Props
Following props are available when using useEntityQuery([entity], [id?])
:
entity: string
: Specify the table name of the entityid?: number | string
: If an id given, it will fetch a single entry, otherwise a list
Returned actions
The following actions are returned from this hook:
create: (input: object)
: Create a new entryread: (query: Partial<RepoQuery> = {})
: If an id was given, it returns a single item, otherwise a listupdate: (input: object, id?: number | string)
: If an id was given, the id parameter is optional. Updates the given entry partially._delete: (id?: number | string)
: If an id was given, the id parameter is optional. Deletes the given entry.
useEntityQuery()
This hook wraps the actions from useEntity
around SWR
. The previous example would look like
this:
Using mutations
All actions returned from useEntityQuery
are conveniently wrapped around the mutate
function,
so you don’t have think about this: