CommentSectionProvider loads and manages all comment state for a given entity. Descendant components access this state through useCommentSection.
Setting Up CommentSectionProvider
Identify the target entity with one of four mutually exclusive props:Provider Props Reference
string
Sublay entity ID. Mutually exclusive with
foreignId, shortId, and entity.string
Your system’s ID for the entity.
string
Auto-generated short ID.
Entity
A pre-fetched entity object. Skips the entity fetch.
boolean
Only valid with
foreignId. Auto-creates the entity if it doesn’t exist yet.number
Comments per page. Default:
15.string
Initial sort order:
"top" (default), "createdAt", or "controversial". "new"/"old" are deprecated aliases for "createdAt" (removed in v8)."asc" | "desc"
Initial sort direction for
sortBy: "createdAt". Default: "desc" (newest first).string
If provided, fetches this comment and its parent and injects it into the tree. Useful for deep-linking to a specific comment.
Record<string, function>
Optional callback functions for handling UI interactions:
loginRequiredCallback()— called when a non-authenticated user tries to commentusernameRequiredCallback()— called when the user has no username setcommentTooShortCallback()— called when the submitted comment is too short
{ user?: string; space?: string }
Override the trigger characters for mentions. Defaults:
{ user: "@", space: "#" }.Accessing State with useCommentSection
useCommentSection Return Values
Entity | null | undefined
The entity these comments belong to.
Comment[]
Paginated root-level comments (not newly submitted ones).
Comment[]
Comments submitted in this session. Displayed separately from paginated results, sorted newest-first.
EntityCommentsTree
The full comment tree as a flat map keyed by comment ID. Used for building threaded reply UIs.
{ comment: Comment; parentComment: Comment | null } | null
The highlighted comment (and its parent, if any) when
highlightedCommentId is set.boolean
true while the initial comment load is in progress.boolean
true when more pages are available.boolean
true while a createComment call is in flight.() => void
Loads the next page of comments.
CommentsSortByOptions | null
Current sort:
"createdAt", "top", or "controversial". ("new"/"old" are deprecated aliases for "createdAt".)(newSortBy: CommentsSortByOptions) => void
Changes the sort order and resets to page 1.
"asc" | "desc"
Current sort direction for
sortBy: "createdAt".(newSortDir: "asc" | "desc") => void
Changes the sort direction and resets to page 1.
(props) => Promise<Comment | undefined>
Submits a new comment. Applies an optimistic placeholder immediately. Accepts:
parentId— reply to a specific comment IDcontent— text contentgif— GIF data objectmentions— array ofMentionobjectsautoReaction— automatically react with this reaction type after creating (e.g."upvote")
(props: { commentId: string; content: string }) => Promise<void>
Updates a comment’s text content.
(props: { commentId: string }) => Promise<void>
Marks a comment as deleted (Reddit-style placeholder). The comment remains in the tree with its content hidden.
Comment | null
Currently selected/focused comment. Managed via
setSelectedComment.(comment: Comment | null) => void
Set or clear the selected comment.
Partial<Comment> | null
The comment currently being replied to.
(comment: Comment | null) => void
Set the comment to reply to.
boolean
Whether to show a reply indicator UI banner.
({ newState }: { newState: boolean }) => void
Show or hide the reply banner.
User | null
A user to auto-insert as a mention in the next comment. Set by
handleShallowReply.(comment: Comment) => void
Sets the reply target for a deep reply (reply appears nested under the parent comment).
(comment: Comment) => void
Sets the reply target for a shallow reply (reply appears at root level with an
@mention).
