From 5f6f8ed5edb2a84591da2cbee0aee3c3652f86cc Mon Sep 17 00:00:00 2001 From: jasinco Date: Wed, 11 Jun 2025 01:41:41 +0800 Subject: [PATCH] Use OnScroll instead of handler --- src/Fetch.tsx | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/Fetch.tsx b/src/Fetch.tsx index 99387b5..38e68f7 100644 --- a/src/Fetch.tsx +++ b/src/Fetch.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef } from "react" +import React from "react" import { FetchPost, MediaPathTrc, SinglePost_t } from "./api" import { z } from 'zod/v4' import { useInfiniteQuery } from "@tanstack/react-query" @@ -7,7 +7,7 @@ import { useInfiniteQuery } from "@tanstack/react-query" const Single = ({ post }: { post: z.infer }) => { return ( -
+

{post.content}

{post.enclosure.length > 0 && post.enclosure[0] != null &&
{post.enclosure.map((enc, index) => { @@ -50,17 +50,15 @@ export const Fetch = () => { return lastPage !== null && lastPage.length > 0 && (lastPage[lastPage.length - 1].id - 1) > 0 ? lastPage[lastPage.length - 1].id - 1 : undefined }, }) - const observer = new IntersectionObserver((ent) => { - - if (ent[0].isIntersecting && ent[0].intersectionRatio > 90) { - hasNextPage && !isFetching && fetchNextPage() - } - }) - useEffect(() => { - observer.observe(document.getElementById("skeleton")!) - }, []) return ( -
+
{ + if (ev.currentTarget.scrollTop / (ev.currentTarget.scrollHeight - ev.currentTarget.clientHeight) >= 0.95) { + if (hasNextPage && !isFetching) { + fetchNextPage() + } + + } + }}> {status === 'pending' &&

Loading

} {status === 'error' &&

Error: {error.message}

} {data && data.pages.map((group, i) => ( @@ -70,7 +68,7 @@ export const Fetch = () => { ))} ))} -
+
{!hasNextPage &&

見底了

}