React hooks to interact with an API from a stateless component using axios.
npm i react-api-hooks -s
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
Param | Type | Default | Description |
---|---|---|---|
url | string | URL that the API call is made to. | |
config | Object | {} | Axios config object passed to the axios.request method. |
paginator | paginatorFunc | offsetPaginator | Function used to update the config object in order to paginate |
responseToItems | function | responseToData | Function used to extract an array of items from response object. |
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]
Param | Type | Description |
---|---|---|
config | Object | Axios config object |
paginationState | Object | Object kept internally to keep track of pagination |
Object
Function used to extract items from the API response.
Kind: global typedef
Returns: Object
- output - Return tuple [items: Object[], hasMore: boolean]
Param | Type | Description |
---|---|---|
response | Object | Axios response object |
Object
The object returned by the useInfAPI hook.
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
items | Array.<Object> | Items provided by the API |
error | Object | undefined | The axios error object is an error occurs. |
isLoading | boolean | Indicates if their is a pending API call for the first page of items. |
isPaging | boolean | Indicates if their is a pending API call for the any page of items. |
setItems | setItemsFunc | Set the items being kept in state |
fetchPage | responseToItemsFunc | Function called from the component in order to fetch the next page |
function
setItems
property of useInfAPIOutput
Kind: global typedef
Param | Type | Description |
---|---|---|
newItems | Array.<Object> | New items array that overwrites current data. |