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:

ParameterTypeRequiredDescription
entityIdstring | null | undefinedNoThe ID of the entity for which comments are being fetched.
userIdstring | null | undefinedNoThe ID of the user whose comments are being fetched.
parentIdstring | null | undefinedNoThe ID of the parent comment for fetching replies.
sortByCommentsSortByOptionsNoThe sorting criteria for comments (e.g., top, new). Defaults to ‘new’.
pagenumberYesThe page number for pagination.
limitnumber | undefinedNoThe number of comments to fetch per page. Default is unlimited.
includeEntityboolean | undefinedNoWhether to include entity details in the response.

Returns

The function resolves with an array of comments:

Return ValueTypeDescription
CommentsComment[]A list of comments matching the criteria.