React API Hooks

React hooks to interact with an API from a stateless component using axios.

npm i react-api-hooks -s
reacthooksAPIaxiosstateless

Reference

useInfAPI(url, config, paginator, responseToItems) ⇒ useInfAPIOutput

React hook used by an infinite scrolling component to make API calls using axios.

Allows you to pass an axios config object, for complete control of the request being sent.

By default it will paginate using a query param offset, and assumes that the API returns an array of items.

If this is not appropriate for your API, then you will need to provide your own paginator and responseToItems functions.

Kind: global function
Returns: useInfAPIOutput - output

ParamTypeDefaultDescription
urlstringURL that the API call is made to.
configObject{}Axios config object passed to the axios.request method.
paginatorpaginatorFuncoffsetPaginatorFunction used to update the config object in order to paginate
responseToItemsfunctionresponseToDataFunction used to extract an array of items from response object.

paginatorFunc ⇒ Array.<Object>

Paginator function used to alter the axios config object, in order to fetch the next page.

Kind: global typedef
Returns: Array.<Object> - output - Return tuple [updatedConfig: Object, updatedPaginationState: Object]

ParamTypeDescription
configObjectAxios config object
paginationStateObjectObject kept internally to keep track of pagination

responseToItemsFunc ⇒ Object

Function used to extract items from the API response.

Kind: global typedef
Returns: Object - output - Return tuple [items: Object[], hasMore: boolean]

ParamTypeDescription
responseObjectAxios response object

useInfAPIOutput : Object

The object returned by the useInfAPI hook.

Kind: global typedef
Properties

NameTypeDescription
itemsArray.<Object>Items provided by the API
errorObject | undefinedThe axios error object is an error occurs.
isLoadingbooleanIndicates if their is a pending API call for the first page of items.
isPagingbooleanIndicates if their is a pending API call for the any page of items.
setItemssetItemsFuncSet the items being kept in state
fetchPageresponseToItemsFuncFunction called from the component in order to fetch the next page

setItemsFunc : function

setItems property of useInfAPIOutput

Kind: global typedef

ParamTypeDescription
newItemsArray.<Object>New items array that overwrites current data.