Skip to content

Instantly share code, notes, and snippets.

@simicd
Created August 16, 2020 18:53
Show Gist options
  • Select an option

  • Save simicd/b9b80b8f8088fb3fbb62f7055de717f2 to your computer and use it in GitHub Desktop.

Select an option

Save simicd/b9b80b8f8088fb3fbb62f7055de717f2 to your computer and use it in GitHub Desktop.
// 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