@anb98/react-hooks
Version:
Custom hooks for react
49 lines • 1.91 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
// import * as React from 'react';
var useLazyFetch_1 = require("./useLazyFetch");
/**
* This hook consumes API when component is mounted and also when calling the fetchHandler function.
By default it request with GET method unless you change initial options.
* @param {string} url URL to request
* @param props Initial options
* @example
* ```
const [state, fetchHandler, resetState ] = useFetch('your-endpoint-url', {
deps: [],
initialData: {},
request: { headers: { example: 'test'} }
onCancel: () => {},
onComplete: (data, err) => {},
onFail: (err) => {},
onSuccess: (data) => {},
});
* ```
* @see https://www.npmjs.com/package/@anb98/react-hooks#useFetch
*/
var useFetch = function (url, props) {
var _a = (props || {}).deps, deps = _a === void 0 ? [] : _a;
return (0, useLazyFetch_1.default)(__assign(__assign({}, props), { url: url, deps: __spreadArray(__spreadArray([], deps, true), [url], false) }));
};
exports.default = useFetch;
//# sourceMappingURL=useFetch.js.map