useFetchSingleComment

Overview

The useFetchSingleComment hook allows you to retrieve a specific comment by its ID. It optionally includes the parent comment, making it useful for displaying threaded comment structures or fetching detailed comment data.

Usage Example

import { useFetchSingleComment } from "@replyke/react-js";
 
function CommentDetails({ commentId }: { commentId: string }) {
  const fetchSingleComment = useFetchSingleComment();
 
  const handleFetchSingleComment = async () => {
    try {
      const { comment, parentComment } = await fetchSingleComment({
        commentId,
        withParent: true,
      });
 
      console.log("Fetched comment:", comment);
      console.log("Parent comment:", parentComment);
    } catch (error) {
      console.error("Failed to fetch comment:", error.message);
    }
  };
 
  return <button onClick={handleFetchSingleComment}>Fetch Comment</button>;
}

Parameters & Returns

Parameters

The hook returns a function that accepts an object with the following fields:

ParameterTypeRequiredDescription
commentIdstringYesThe ID of the comment to fetch.
withParentbooleanNoWhether to include the parent comment.

Returns

The function resolves with an object containing the comment and optionally its parent comment:

Return ValueTypeDescription
commentCommentThe details of the fetched comment.
parentCommentComment | nullThe parent comment, if requested.