UNPKG

use-fetch-url

Version:

A custom hooks in TypeScript for fetching data from a URL and managing its state, including status and error information.

1 lines 2.08 kB
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("react"));else if("function"==typeof define&&define.amd)define(["react"],t);else{var r="object"==typeof exports?t(require("react")):t(e.react);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(self,(e=>(()=>{"use strict";var t={499:(e,t)=>{var r;Object.defineProperty(t,"__esModule",{value:!0}),t.FetchStatus=void 0,(r=t.FetchStatus||(t.FetchStatus={})).LOADING="LOADING",r.COMPLETE="COMPLETE",r.ERROR="ERROR"},4:function(e,t,r){var n=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,u){function c(e){try{a(n.next(e))}catch(e){u(e)}}function s(e){try{a(n.throw(e))}catch(e){u(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(c,s)}a((n=n.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.useFetchUrl=t.useFetch=void 0;const o=r(156),u=r(499);function c(e,t=null){const{LOADING:r,COMPLETE:c,ERROR:s}=u.FetchStatus,[a,i]=(0,o.useState)(t),[f,l]=(0,o.useState)(r),[d,h]=(0,o.useState)(null),v=()=>n(this,void 0,void 0,(function*(){e().then((e=>{i(e),l(c)})).catch((e=>{console.error(e),h(e),l(s)}))}));return(0,o.useEffect)((()=>{v()}),[]),{data:a,status:f,error:d}}t.useFetch=c,t.useFetchUrl=function(e,t=null){const r=e=>n(this,void 0,void 0,(function*(){const t=yield fetch(e);return yield t.json()})),{data:o,status:u,error:s}=c((()=>r(e)),t);return{data:o,status:u,error:s}}},156:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var u=r[e]={exports:{}};return t[e].call(u.exports,u,u.exports,n),u.exports}var o={};return(()=>{var e=o;Object.defineProperty(e,"__esModule",{value:!0}),e.FetchStatus=e.useFetchUrl=e.useFetch=void 0;var t=n(4);Object.defineProperty(e,"useFetch",{enumerable:!0,get:function(){return t.useFetch}}),Object.defineProperty(e,"useFetchUrl",{enumerable:!0,get:function(){return t.useFetchUrl}});var r=n(499);Object.defineProperty(e,"FetchStatus",{enumerable:!0,get:function(){return r.FetchStatus}})})(),o})()));