Skip to main content

useEntityComments

Overview

The useEntityComments hook provides a higher-level abstraction for fetching and managing comments related to a specific entity. It handles pagination, sorting, and comment tree structure, allowing you to easily build a threaded comment UI. It’s especially useful when you want to render nested replies and dynamically manage additions or deletions in a structured way.

Usage Example

Parameters & Returns

Parameters

The hook accepts an object with the following fields:
string \
null`
number
The number of comments to fetch per page. Default is 10.
CommentsSortByOptions
The default sorting criteria for comments (e.g., "new", "top").

Returns

The hook returns an object with the following fields:
EntityCommentsTree
The structured tree of comments, keyed by comment ID.
Comment[]
The list of root-level comments (non-new).
Comment[]
Comments recently added during the session.
boolean
Indicates whether comments are currently loading.
boolean
Whether there are more comments available to fetch.
CommentsSortByOptions
Current sorting method for the comments ("new" or "top").
(newSortBy: CommentsSortByOptions) => void
Updates the sort order for comments.
() => void
Loads the next page of comments, if available.
(newComments: Comment[] \
undefined, newlyAdded?: boolean) => void`
(commentId: string) => void
Removes a comment (and its descendants) from the tree structure.