Skip to content

onReact

Patches may optionally define an onReact function that will be called when a user reacts with an emoji to the patch message. onReact should be defined at the top level of the patch code, outside of any other functions.

The function is passed an event object with the following properties:

function onReact(e: {
user: SupUser;
reactionEmoji: {
id: string;
shortname: string;
imageUrl?: string;
audioUrl?: string;
};
}) => void;

Basic Usage

function onReact(e) {
console.log(`${e.user.username} reacted: ${e.reactionEmoji.shortname}`);
}

Updating the initial message

The main function will be re-run every time onReact completes, so you can use onReact to update state or perform other side effects in response to user reactions. This will update the initial message with the new content.

function main() {
const reactionCount = sup.get("reactionCount") || 0;
return `This message has received ${reactionCount} reactions`;
}
function onReact(e) {
let count = sup.get("reactionCount") || 0;
count++;
sup.set("reactionCount", count);
}