useFetchComments

Overview

The useFetchComments 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 { useFetchComments } from "@replyke/react-js";
 
function CommentsList({ entityId }: { entityId: string }) {
  const fetchComments = useFetchComments();
 
  const handleFetchComments = async () => {
    try {
      const comments = await fetchComments({
        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.
sortByCommentsSortByOptionsYesThe sorting criteria for comments (e.g., top, 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
CommentType[]CommentType[]A list of comments matching the criteria.