ZL
About Articles Contact
Published on Jan 25, 2026
Filed under:
#astro,
#splendidlabz

If and Not Components for Astro

I like using Astro, but one thing that consistently irritates me is the lack of an if statement. Instead of a simple if, we have to use a JSX conditional like this.

{
condition && (
<!-- Markup goes here -->
)
}

This is annoying because the condition takes up space and creates indentation, which makes the code harder to parse.

It’s a stark contrast to Vue’s and Svelte’s if statements.

index.vue
<div v-if={condition}>
<!-- Markup goes here -->
</div>
index.svelte
{#if}
<!-- Markup goes here -->
{/if}

After some experiments, I created an If component that simplifies JSX code above.

index.astro
<If condition={1 + 1 === 2}>
<!-- Markup goes here -->
</If>

And a Not component that does the opposite.

index.astro
<Not condition={authorized}>
<!-- Markup goes here -->
</Not>

Building these yourself

There’s nothing complex about If and Not. You need to check for a condition and pass a slot as the markup.

If.astro
---
const { condition } = Astro.props
---
{condition && <slot />}

Caveats

There’s one major caveat you need to know when using If and Not components in Astro.

Astro evaluates slot content before passing them into the components. It doesn’t take your condition into account. This means you can’t access undefined properties in slot.

index.astro
---
const stuff = undefined
---
<If condition={stuff}>
<!-- This will result in an error since undefined cannot have properties -->
{stuff.property}
</If>

Luckily, you can use optional chaining for these situations:

index.astro
<If condition={stuff}>
{stuff.property}
{stuff?.property}
</If>

Using If and Not via Splendid Labz

I added If and Not into Splendid Astro. You can import and use them directly.

---
import {If, Not} from '@splendidlabz/astro'
---
<If condition={...}> <!-- Markup goes here --> </If>
<Not condition={...}> <!-- Markup goes here --> </Not>

Both If and Not have a few improvements over the barebones code I provided above.

Improvements

If / Else

It’s quite common to have an if/else branch. You can use Not for this, but you need to pass in the condition again, so it’s not ideal.

---
const authorized = /* ... */
---
<If condition={authorized}> ... </If>
<Not condition={authorized}> ... </If>

So If takes an else slot that renders when the condition is falsey.

<If condition={authorized}>
<div>
<!-- Default slot. Shows this when truthy -->
</div>
<div slot="else">
<!-- Else slot. Shows this when falsey -->
</div>
</If>

Not has the same feature as well — it takes an else slot that renders when the condition is truthy. (Though pou should probably use the If component for this sorta thing, because If is easier to parse than Not).

<Not condition={authorized}>
<div>
<!-- Default slot. Shows this when falsey -->
</div>
<div slot="else">
<!-- Else slot. Shows this when truthy -->
</div>
</If>

True and False slots

The Default and else slots may introduce some level of confusion if you have a lot of markup. To make things clearer, I introduced true and false slots as their aliases.

  • true slot acts as the default slot
  • false slot acts as else slot
<If condition={authorized}>
<div slot="true">
<!-- Shows this when truthy -->
</div>
<div slot="false">
<!-- Shows this when falsey -->
</div>
</If>

Of course, Not has the same feature as well.

<Not condition={authorized}>
<div slot="true">
<!-- Shows this when falsey -->
</div>
<div slot="false">
<!-- Shows this when truthy -->
</div>
</If>

These are just extra affordances, so feel free to use whatever flavour you prefer!

Hope you find these components useful!

Further reading

  • If component documentation
  • Not component documentation
Previous Closing off the reflections about 'violence' Next Everyone Loves This!

Join My Newsletter

I share what I’m learning on this newsletter: code, building businesses, and living well.

Sometimes I write about technical deep-dives, product updates, musings on how to live, and sometimes my struggles and how I’m breaking through.

Regardless of the type of content, I do my best to send you an update every week.

If you’re into making things and growing as a person, you’ll probably feel at home here.

“

Zell is one of those rare people who commands tremendous knowledge and experience but remains humble and helpful. They want you to know what they know, not just be impressed by it.

In other words, Zell is a natural teacher. You’re lucky to have him because he feels lucky to be able to help you in your journey.

Heydon Pickering
Heydon Pickering — Web & Accessibility Extraordinaire
The Footer

General

Home About Contact Testimonials Tools I Use

Projects

Magical Dev School Splendid Labz

Socials

Youtube Instagram Tiktok Github Bluesky X

Follow Along

Email RSS
© 2013 - 2026 Zell Liew / All rights reserved / Terms