( url )
| 2 | |
| 3 | |
| 4 | export const useFetch = ( url ) => { |
| 5 | |
| 6 | const [state, setState] = useState({ |
| 7 | data: null, |
| 8 | isLoading: true, |
| 9 | hasError: null, |
| 10 | }) |
| 11 | |
| 12 | |
| 13 | const getFetch = async () => { |
| 14 | |
| 15 | setState({ |
| 16 | ...state, |
| 17 | isLoading: true, |
| 18 | }); |
| 19 | |
| 20 | const resp = await fetch(url); |
| 21 | const data = await resp.json(); |
| 22 | |
| 23 | setState({ |
| 24 | data, |
| 25 | isLoading: false, |
| 26 | hasError: null, |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | useEffect(() => { |
| 32 | getFetch(); |
| 33 | }, [url]) |
| 34 | |
| 35 | |
| 36 | |
| 37 | return { |
| 38 | data: state.data, |
| 39 | isLoading: state.isLoading, |
| 40 | hasError: state.hasError, |
| 41 | }; |
| 42 | } |
no test coverage detected