This file contains 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
// DogImage.tsx | |
import React, { FC } from "react"; | |
import { useFetch } from "./useFetch"; | |
type DogImageType = { message: string; status: string }; | |
export const DogImage: FC = () => { | |
const data = useFetch<DogImageType>({ | |
url: "https://dog.ceo/api/breed/beagle/images/random" | |
}); |
This file contains 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"; | |
interface RequestProps<T> { | |
url: RequestInfo; | |
init?: RequestInit; | |
processData?: (data: any) => T; | |
} | |
export const useFetch = <T>({ url, init, processData }: RequestProps<T>) => { |
This file contains 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 |
This file contains 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 } from "react"; | |
interface RequestProps { | |
url: RequestInfo; | |
init?: RequestInit; | |
processData?: (data: any) => DogImageType | |
} | |
export type DogImageType = { message: string; status: string }; |
This file contains 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
// DogImage.tsx | |
import React, { FC } from "react"; | |
import { useFetch, DogImageType } from "./useFetch"; | |
export const DogImage: FC = () => { | |
const data = useFetch({ | |
url: "https://dog.ceo/api/breed/beagle/images/random", | |
processData: (data) => data as DogImageType, | |
}); |
This file contains 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 } from "react"; | |
// .. | |
export const useFetch = ({ url, init }: RequestProps) => { | |
// ... | |
// Turn objects into strings for useCallback & useEffect dependencies |
This file contains 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 | |
// .. | |
export const useFetch = ({ url, init }: RequestProps) => { | |
// Response state | |
// .. | |
useEffect(() => { | |
// ... |
This file contains 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
// DogImage.tsx | |
import React, { FC } from "react"; | |
import { useFetch } from "./useFetch"; | |
export const DogImage: FC = () => { | |
const data = useFetch({ url: "https://dog.ceo/api/breed/beagle/images/random", init: {}}) | |
return <>{data ? <img src={data.message} alt="dog"></img> : <div>Loading</div>}</>; | |
}; |
This file contains 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 | |
// ... | |
export const useFetch = ({ url, init }: RequestProps) => { | |
// Response state | |
const [data, setData] = useState<DogImageType>(); | |
useEffect(() => { | |
// Define asynchronous function |
This file contains 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
//DogImage.tsx | |
import React, { FC } from "react"; | |
import { useFetch } from "./useFetch"; | |
export const DogImage: FC = () => { | |
const data = useFetch({ url: "https://dog.ceo/api/breed/beagle/images/random"}) | |
return <>{data ? <img src={data.message} alt="dog"></img> : <div>Loading</div>}</>; | |
}; |