Skip to main content

useFetchComments

Overview

The useFetchManyComments hook allows you to retrieve a list of comments based on various filters, such as the associated entity, user, or parent comment. It supports sorting, pagination, and optional inclusion of the related entity data.

Usage Example

import { useFetchManyComments } from "@replyke/react-js";

function CommentsList({ entityId }: { entityId: string }) {
  const fetchManyComments = useFetchManyComments();

  const handleFetchComments = async () => {
    try {
      const comments = await fetchManyComments({
        entityId,
        sortBy: "top",
        page: 1,
        limit: 10,
        includeEntity: false,
      });

      console.log("Fetched comments:", comments);
    } catch (error) {
      console.error("Failed to fetch comments:", error.message);
    }
  };

  return <button onClick={handleFetchComments}>Fetch Comments</button>;
}

Parameters & Returns

Parameters

The hook returns a function that accepts an object with the following fields:
entityId
string \
undefined`
userId
string \
undefined`
parentId
string \
undefined`
sortBy
CommentsSortByOptions
The sorting criteria for comments (e.g., top, new). Defaults to ‘new’.
page
number
required
The page number for pagination.
limit
number \
No
includeEntity
boolean \
No

Returns

The function resolves with an array of comments:
Comments
Comment[]
A list of comments matching the criteria.
I