Overview
The useCommentSectionData hook provides a comprehensive solution for managing comment sections associated with entities. Unlike individual hooks such as useFetchComments or useCreateComment, this hook combines functionalities like fetching, creating, updating, deleting, and managing comment states (e.g., sorting, replying). It is ideal for implementing fully-featured comment sections with advanced state management.
Usage Example
Parameters & Returns
Parameters
The hook accepts an object with the fields below. At least one of the first 4 fields must be provided (entityId/foreignId/shortId/entity):
The number of comments to fetch per page. Default is 15.
The default sorting criteria for comments (e.g., new, top).
Optional callbacks for handling events like login requirements.
Returns
The hook returns an object with the following fields:
callbacks Record<string, (...args: any[]) => void> \
undefined`
Tree structure representing threaded comments.
The list of root-level comments currently loaded.
Comments that were recently added during the session.
Indicates if comments are currently loading.
Whether there are more comments available for pagination.
Whether a comment is currently being submitted.
Loads the next page of comments.
setSortBy (newSortBy: CommentsSortByOptions) => void
Updates the comment sort order.
Whether the UI banner indicating a reply is visible.
setShowReplyBanner (newState: boolean) => void
Controls the reply banner visibility.
undefined, newlyAdded?: boolean) => void`
Removes a comment from the tree structure.
handleShallowReply (comment: Comment) => void
Handles a shallow reply (mention-only, not nested).
handleDeepReply (comment: Comment) => void
Handles a deep reply (visibly nested under the parent comment).
Submits a new comment. Includes temporary rendering and error fallback.
Updates an existing comment.
Deletes a comment from both the UI and backend.