Skip to main content

Overview

useReactionToggle is the recommended hook for reaction UIs. It manages the current reaction and reaction counts locally with optimistic updates, handles toggling (selecting the same reaction removes it), and reverts on error. It works for both entities and comments via the targetType prop.

Usage Example

Props

"entity" | "comment"
required
Whether the reaction target is an entity or a comment.
string | undefined
required
The ID of the entity or comment.
ReactionType | null
The authenticated user’s current reaction, if any. Sourced from entity.userReaction or comment.userReaction.
ReactionCounts | null
The current reaction counts object. Sourced from entity.reactionCounts or comment.reactionCounts.

Return Values

ReactionType | null
The authenticated user’s current reaction. Updates optimistically on toggleReaction.
Partial<ReactionCounts>
The current reaction counts. Updated optimistically and corrected from server response.
(props: { reactionType: ReactionType }) => Promise<Entity | Comment | null>
Toggles the given reaction. If the user already has this reaction, it is removed. If a different reaction is active, it is replaced. Returns the updated entity or comment from the server, or null on error.
boolean
true while a toggle request is in flight. Prevents concurrent reactions.