React & React NativeHooksCommentsuseCreateComment

useCreateComment

Overview

The useCreateComment hook allows users to add a new comment to an entity or reply to an existing comment. It supports optional mentions within the comment content, enabling tagging other users.

Usage Example

import { useCreateComment } from "@replyke/react-js";
 
function AddComment({ entityId }: { entityId: string }) {
  const createComment = useCreateComment();
 
  const handleAddComment = async () => {
    try {
      const newComment = await createComment({
        entityId,
        content: "This is a new comment!",
        mentions: [{ userId: "123", username: "JohnDoe" }],
      });
 
      console.log("Comment added successfully:", newComment);
    } catch (error) {
      console.error("Failed to add comment:", error.message);
    }
  };
 
  return <button onClick={handleAddComment}>Add Comment</button>;
}

Parameters & Returns

Parameters

The hook returns a function that accepts an object with the following fields:

ParameterTypeRequiredDescription
entityIdstringYesThe ID of the entity to which the comment is added.
foreignIdstringNoIf the comment is imported from an external dataset, use this to pass its ID.
parentCommentIdstring | nullNoThe ID of the parent comment (for replies).
contentstringNoThe text content of the comment.
gifGifDataNoObject containing information about the GIF.
mentionsMention[]NoAn array of mentions to include in the comment.
referencedCommentIdstringNoIf this comment references another comment, this will be the ID of th referenced comment. Different than parentCommentId which is used for threaded replies.
attachmentsRecord<string, any>[]NoData about any attachments which were part of the comment.
metadataRecord<string, any>NoExtra free form metadata.

Returns

The function resolves with the newly created comment object:

Return ValueTypeDescription
responseCommentThe details of the newly created comment.