Create a vote
Create a vote in Bitsocial React Hooks. Usage, options, and examples.
const commentCid = "QmZVYzLChjKrYDVty6e5JokKffGDZivmEJz9318EYfp2ui";
const publishVoteOptions = {
commentCid,
vote: 1,
communityAddress: "news.eth",
onChallenge,
onChallengeVerification,
onError,
};
const { state, error, publishVote, abandonPublish } = usePublishVote(publishVoteOptions);
await publishVote();
console.log(state);
console.log(error);
// if the user closes the challenge modal and wants to cancel voting:
await abandonPublish();
// the vote publication is stopped and the account vote goes back to what it was
// before publishVote(), so the optimistic vote does not survive a cancelled challenge
// display the user's vote
const { vote } = useAccountVote({ commentCid });
if (vote === 1) console.log("user voted 1");
if (vote === -1) console.log("user voted -1");
if (vote === 0) console.log("user voted 0");
if (vote === undefined) console.log(`user didn't vote yet`);
useComment, useComments, useReplies, and useFeed apply the selected account’s pending vote
to upvoteCount and downvoteCount immediately, so a score updates the same way whether the vote
was cast from a feed or from a comment page. The optimistic adjustment is replaced by the next
canonical comment update.