Skip to content

Commit 4694627

Browse files
committed
Update hooks documentation
1 parent 463fc85 commit 4694627

File tree

5 files changed

+46
-18
lines changed

5 files changed

+46
-18
lines changed

generate-reference.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ function generateReferenceDoc(inputFl, outputFl) {
1414

1515
generateReferenceDoc('src/useAPI.js', 'website/reference/use-api.md');
1616
generateReferenceDoc('src/useParams.js', 'website/reference/use-params.md');
17+
generateReferenceDoc('src/useInfAPI.js', 'website/reference/use-inf-api.md');

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as useAPI } from './useAPI';
22
export { default as useParams } from './useParams';
3+
export { default as useInfAPI } from './useInfAPI';

src/useAPI.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { useEffect, useState } from 'react';
22
import axios from 'axios';
33
import hash from 'hash-object';
44

5-
const CancelToken = axios.CancelToken;
5+
const { CancelToken } = axios;
66

77
/**
88
* The object returned by the useAPI hook.
99
* @typedef {Object} useAPIOutput
1010
* @property {Object|undefined} data - The data attribute from the axios response.
1111
* @property {Object|undefined} response - The axios response.
12-
* @property {Object|undefined} error - The axios error object is an error occurs.
12+
* @property {Object|undefined} error - The axios error object if an error occurs.
1313
* @property {boolean} isLoading - Indicates if their is a pending API call.
1414
* @property {setDataFunc} setData - Set the response data object.
1515
*/

src/useInfAPI.js

+41-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import { offsetPaginator, responseToData } from './utils';
55

66
const { CancelToken } = axios;
77

8+
/**
9+
* Paginator function used to alter the axios config object, in order to fetch the next page.
10+
* @typedef {function} paginatorFunc
11+
* @param config {Object} - Axios config object
12+
* @param paginationState {Object} - Object kept internally to keep track of pagination
13+
* @return output {Object[]} - Return tuple \[updatedConfig: Object, updatedPaginationState: Object\]
14+
*/
15+
16+
/**
17+
* Function used to extract items from the API response.
18+
* @typedef {function} responseToItemsFunc
19+
* @param response {Object} - Axios response object
20+
* @return output {Object} - Return tuple \[items: Object[], hasMore: boolean\]
21+
*/
22+
823
/**
924
* The object returned by the useInfAPI hook.
1025
* @typedef {Object} useInfAPIOutput
@@ -13,7 +28,7 @@ const { CancelToken } = axios;
1328
* @property {boolean} isLoading - Indicates if their is a pending API call for the **first** page of items.
1429
* @property {boolean} isPaging - Indicates if their is a pending API call for the **any** page of items.
1530
* @property {setItemsFunc} setItems - Set the items being kept in state
16-
* @property {function} fetchMore - Function called from the component in order to fetch the next page
31+
* @property {responseToItemsFunc} fetchPage - Function called from the component in order to fetch the next page
1732
*/
1833

1934
/**
@@ -27,10 +42,14 @@ const { CancelToken } = axios;
2742
*
2843
* Allows you to pass an [axios config object](https://github.com/axios/axios#request-config), for complete control of the request being sent.
2944
*
45+
* By default it will paginate using a query param `offset`, and assumes that the API returns an array of items.
46+
*
47+
* If this is not appropriate for your API, then you will need to provide your own `paginator` and `responseToItems` functions.
48+
*
3049
* @param {string} url - URL that the API call is made to.
3150
* @param {Object} config={} - Axios config object passed to the axios.request method.
32-
* @param {function} paginator= - Function used to update the config object in order to paginate
33-
* @param {function} responseToItems= - Function used to extract an array of items from response object.
51+
* @param {paginatorFunc} paginator=offsetPaginator - Function used to update the config object in order to paginate
52+
* @param {function} responseToItems=responseToData - Function used to extract an array of items from response object.
3453
* @returns {useInfAPIOutput} output
3554
*/
3655
function useInfAPI(
@@ -45,33 +64,42 @@ function useInfAPI(
4564
error: undefined,
4665
isLoading: true,
4766
isPaging: true,
48-
hasMore: false
67+
hasMore: false,
68+
source: CancelToken.source()
4969
});
5070

5171
const configHash = hash(config);
5272

5373
const { items, paginationState } = state;
5474

55-
function callAPI(cancelToken, isLoading) {
75+
function callAPI(isLoading) {
5676
const activePaginationState = isLoading ? {} : paginationState; // Reset pagination when config object changes.
5777
const [updatedConfig, updatedPaginationState] = paginator(config, activePaginationState);
5878

59-
setState({ ...state, isLoading, isPaging: true, paginationState: updatedPaginationState });
79+
setState({
80+
...state,
81+
isLoading,
82+
isPaging: true,
83+
paginationState: updatedPaginationState
84+
});
6085
axios(url, {
6186
...updatedConfig,
62-
cancelToken
87+
cancelToken: state.source.token
6388
})
6489
.then(response => {
6590
const [pageItems, hasMore] = responseToItems(response);
66-
if (pageItems instanceof []) {
91+
if (typeof pageItems === typeof []) {
6792
setState({
6893
...state,
6994
items: items.concat(pageItems),
7095
error: undefined,
7196
isLoading: false,
7297
isPaging: false,
73-
hasMore
98+
hasMore,
99+
paginationState: updatedPaginationState
74100
});
101+
} else {
102+
console.log("Warning: responseToItems didn't return an array.");
75103
}
76104
})
77105
.catch(error => {
@@ -83,25 +111,23 @@ function useInfAPI(
83111
});
84112
}
85113

86-
let source;
87114
useEffect(() => {
88-
source = CancelToken.source();
89-
callAPI(source.token, true);
115+
setState({ ...state, source: CancelToken.source() });
116+
callAPI(true);
90117
return () => {
91-
source.cancel('useEffect cleanup.');
118+
state.source.cancel('useEffect cleanup.');
92119
};
93120
}, [url, configHash]);
94121

95122
const { error, isPaging, isLoading, hasMore } = state;
96-
97123
return {
98124
items,
99125
error,
100126
isPaging,
101127
isLoading,
102128
hasMore,
103129
setItems: newItems => setState({ ...state, items: newItems }),
104-
fetchPage: () => callAPI(source.token, false)
130+
fetchPage: () => callAPI(false)
105131
};
106132
}
107133

src/useParams.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import debounce from 'lodash.debounce';
2727
/**
2828
* React hook to keep query parameters in state.
2929
*
30-
* Used in conjunction with the other hooks to filter and pagination API calls.
30+
* Used in conjunction with the other hooks to filter and paginate APIs.
3131
*
3232
* Includes the ability the debounce an update, which is useful for delaying API calls while the user is typing.
3333
*

0 commit comments

Comments
 (0)