Created
August 16, 2020 18:53
-
-
Save simicd/b9b80b8f8088fb3fbb62f7055de717f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // useFetch.ts | |
| import { useState, useEffect, useCallback } from "react"; | |
| // ... | |
| export const useFetch = ({ url, init, processData }: RequestProps) => { | |
| // ... | |
| // The callback hook ensures that the function is only created once | |
| // and hence the effect hook below doesn't start an infinite loop | |
| const processJson = useCallback(processData || ((jsonBody: any) => jsonBody as DogImageType), []); | |
| useEffect(() => { | |
| // ... | |
| const processedData = processJson(rawData); | |
| // ... | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, [stringifiedUrl, stringifiedInit, processJson]); | |
| return data; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment