import { Text, View } from "@tarojs/components"; import CommentListItem from "./item"; import { useEffect, useState } from "react"; import { getCommentListApi } from "../../api/comment"; const CommentList = ({ postId }) => { // const CommentList = ({ commentsData }) => { const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const [commentsData, setCommentsData] = useState([]); const [count, setCount] = useState(0); useEffect(() => { getCommentListApi({ post_id: postId }).then((res: any) => { if (res.code === 200) { setCommentsData(res.data.list); setCount(res.data.count); setHasMore(res.data.has_more); } }); }, [postId]); // 加载更多 const handleLoadMore = () => { setPage(page + 1); }; useEffect(() => { getCommentListApi({ post_id: postId, page: page }).then((res: any) => { if (res.code === 200) { setCommentsData([...commentsData, ...res.data.list]); } }); }, [page]); return ( 共 {count} 条评论 {hasMore && ( { handleLoadMore(); }} > 加载更多 )} ); }; export default CommentList;