Skip to main content

Data retrieval

Oneki.js provides several hooks to retrieve data from a server via an AJAX GET request

Hooks

const [data, loading, refresh] = useGet(url, options);
const [data, loading, refresh] = useSecureGet(url, options);
const [data, loading, refresh] = useCache(url, options);

useGet hook executes an ajax GET request and returns back the data.
The ajax request is made during the mount of the component and each time the URL is updated.
The data is stored in the component state and not in the redux store

useSecureGet is similar to useGet but adds a authorization header Bearer which contains the token received and stored by useLoginService in the redux store

useCache hook is similar to useGet except that the data is cached in the redux store (one entry in the store per URL)
Therefore, the ajax request is only executed if the cache is not yet present in the store or if the cache is no longer valid.

Parameters

Inputs

Mandatory parameters are marked with a *

ParameterTypeDescription
url*stringthe URL to which the Ajax request is sent
optionsAn optional object to specify additional options
onErrorAppErrorCallback
  • if onError is a function, this function is called if the HTTP response is a 4xx or 5xx (Promise / async allowed)
  • if onError is a string then the value must be an URL. The hook does a redirect to this URL
Defaults to: the hook sends a notification to the error topic
delayLoadingnumberThe number of milliseconds to wait before setting the loading flag to true. This value is useful to not display a loading indicator if the request is executed rapidly.

Defaults to: 0
defaultValueT(only supported by useGet). The value returned while the AJAX request is pending.

Defaults to: undefined
authobjectContains the credentials to be sent in the request. See Auth.

Defaults to: no credentials sent
headersobjectthe HTTP headers to add in the request

Defaults to: no headers added in the request
paramsobjectParams in the url.
Example: if the user is /user/:userId, options.params could be {userId: '1'}
queryobjectQuery params to add the URL.
Example: If the final url is /users?name=Doe, options.query will be {name: 'Doe'}
note

The options object also accept these additionnal parameters supported by the fetch API

headers,
credentials,
cache,
redirect,
referrer,
referrerPolicy,
integrity,
keepalive,
signal

Outputs

ParameterTypeDescription
dataTthe data returned by the server.
The value is null while the ajax request is pending
loadingbooleanA flag to indicate if the ajax request is pending
refresh() => voida function that can be called to force a re-execution of useGet

Examples

Minimal example

The minimal example shows how to retrieve data from a backend server.
A loading indicator (Loading ...) is displayed while the AJAX request is pending.

examples/cra-data/src/pages/list-users.tsx
loading...

Cache example

examples/cra-data/src/pages/list-users-cache.tsx
loading...

Polling example

examples/cra-data/src/pages/list-users-pooling.tsx
loading...

loading delay example

Show a loading indicator only if the data is not retrieved in less than 100 ms.

examples/cra-data/src/pages/list-users-delay.tsx
loading...

onError example

This is an example showing how to use the onError handler

examples/cra-data/src/pages/list-users-on-error.tsx
loading...

onError with notification example

This example shows how to handle onError using the notification service

examples/cra-data/src/pages/list-users-error-notification.tsx
loading...