Next.js Discord

Discord Forum

Conditionally rendering <Image> in Next.js

Answered
neon posted this in #help-forum
Open in Discord
How do I render an image (using next/image library) only if a certain variable is true? I tried
{myCondition ?? (
  <Image
    src={vector}
    alt="vector"
    width={550}
    className="absolute left-[60%] justify-center"
  />
)}

But this way, the image just doesn't load even if myCondition is true.
Answered by B33fb0n3
try to do it like this:
{myCondition && <Image
    src={vector}
    alt="vector"
    width={550}
    className="absolute left-[60%] justify-center"
  />}
View full answer

4 Replies

Orinoco Crocodile
I think you want to use &&
Answer