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
| // DogImage.tsx | |
| import React, { FC, useState, useEffect } from "react"; | |
| export const DogImage: FC = () => { | |
| type DogImageType = { message: string; status: string }; | |
| const [data, setData] = useState<DogImageType>(); | |
| useEffect(() => { | |
| // Define asynchronous function - since useEffect hook can't handle async directly, |
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 } from "react"; | |
| interface RequestProps { | |
| url: RequestInfo; | |
| init?: RequestInit; | |
| } | |
| type DogImageType = { message: string; status: string }; |
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 | |
| // ... | |
| export const useFetch = ({ url, init }: RequestProps) => { | |
| // Response state | |
| const [data, setData] = useState<DogImageType>(); | |
| useEffect(() => { | |
| // Define asynchronous function |
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
| //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>}</>; | |
| }; |
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 | |
| // ... | |
| export const useFetch = ({ url, init }: RequestProps) => { | |
| // Response state | |
| const [data, setData] = useState<DogImageType>(); | |
| useEffect(() => { | |
| // Define asynchronous function |
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
| // 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 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 | |
| // .. | |
| export const useFetch = ({ url, init }: RequestProps) => { | |
| // Response state | |
| // .. | |
| useEffect(() => { | |
| // ... |
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 } from "react"; | |
| // .. | |
| export const useFetch = ({ url, init }: RequestProps) => { | |
| // ... | |
| // Turn objects into strings for useCallback & useEffect dependencies |
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
| // 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 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 } from "react"; | |
| interface RequestProps { | |
| url: RequestInfo; | |
| init?: RequestInit; | |
| processData?: (data: any) => DogImageType | |
| } | |
| export type DogImageType = { message: string; status: string }; |