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:
Parameter | Type | Required | Description |
---|---|---|---|
entityId | string | null | undefined | No | The ID of the entity for which comments are being fetched. |
userId | string | null | undefined | No | The ID of the user whose comments are being fetched. |
parentId | string | null | undefined | No | The ID of the parent comment for fetching replies. |
sortBy | CommentsSortByOptions | Yes | The sorting criteria for comments (e.g., top , new ). |
page | number | Yes | The page number for pagination. |
limit | number | undefined | No | The number of comments to fetch per page. Default is unlimited. |
includeEntity | boolean | undefined | No | Whether to include entity details in the response. |
Returns
The function resolves with an array of comments:
Return Value | Type | Description |
---|---|---|
CommentType[] | CommentType[] | A list of comments matching the criteria. |