diff --git a/package.json b/package.json index 9c606fe..01a99a1 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "next-seo": "^5.1.0", "react": "^17.0.2", "react-dom": "^17.0.2", + "react-intersection-observer": "^8.33.1", "react-query": "^3.34.16" }, "devDependencies": { diff --git a/src/components/Video/index.tsx b/src/components/Video/index.tsx index ec9538f..c55c1c7 100644 --- a/src/components/Video/index.tsx +++ b/src/components/Video/index.tsx @@ -3,8 +3,9 @@ import axios, { AxiosError } from "axios"; import Link from "next/link"; import { useRouter } from "next/router"; -import { FC } from "react"; +import { FC, useEffect } from "react"; +import { useInView } from "react-intersection-observer/"; import { useQuery } from "react-query"; import Avatar from "@mui/material/Avatar"; @@ -35,7 +36,10 @@ const Video: FC = ({ }) => { const requestFields = ["authorThumbnails"]; - const { isLoading, error, data } = useQuery>( + const { isLoading, error, data, refetch, isFetched } = useQuery< + Channel, + AxiosError + >( ["channelData", author.id], () => axios @@ -44,11 +48,20 @@ const Video: FC = ({ fields: requestFields.join(",") } }) - .then((res) => res.data) + .then((res) => res.data), + { enabled: false } ); + const { ref, inView } = useInView({ + threshold: 0 + }); + const router = useRouter(); + useEffect(() => { + if (inView && !isFetched) refetch(); + }, [inView, isFetched, refetch]); + return ( = ({ - + {isLoading && } {data && (