Skip to content

Instantly share code, notes, and snippets.

@richleach
Created April 6, 2022 23:00
Show Gist options
  • Save richleach/a6043577206977883a70dca25322c5fd to your computer and use it in GitHub Desktop.
Save richleach/a6043577206977883a70dca25322c5fd to your computer and use it in GitHub Desktop.
Run and print results of JS inside the actual return statement of a Next.js React component. Parse on-demand, inside the JSX
//in this sample, assume an axios get request returned a number between 1 and 5 which was stored in state (stAffection).
//instead of parsing it out above the return block and possibly creating another variable, this is a way to do it "inline"
return (
<>
//...HTML formatting stuff, maybe some output, mapping, etc.
<h3 className="m-2"><span className="font-semibold">Affection level: </span>
{(() => {
switch (stAffection) {
case 5: return " Most affectionate";
case 4: return " Will tolerate you";
case 3: return " Slightly demonic";
case 2: return " Will cut you";
case 1: return " Kitty de Lucifer";
default: return " grumpy";
}
})()}
</h3>
//... maybe some more HTML stuff...
</>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment