🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

Astro Dynamic Components and Tag Names

Render Astro dynamic components and tags from a variable: polymorphic as props, heading levels, a block-type component map, and the hydration directive limit.

#Astro#Dynamic Components#Components#TypeScript

To render a dynamic tag or component in Astro, assign it to a capitalised variable in the frontmatter and use that variable as the tag: const Tag = href ? 'a' : 'button' then <Tag href={href}>. The same works for components, const Block = blocks[type] then <Block {...props} />, which is how you render page builders and CMS content. The variable must start with a capital letter, and hydration directives don’t work on Astro dynamic components.

Key Takeaways

  • Capitalise the variable: <Tag> is dynamic, <tag> is a literal HTML element called “tag”.
  • Strings render HTML elements; imported components render components.
  • A lookup object maps CMS block types to components without a long if chain.
  • client:* directives aren’t supported on dynamic component variables; import islands directly.

A polymorphic button

A component that renders a link when it has an href, and a button otherwise:

---
// src/components/Button.astro
import type { HTMLAttributes } from 'astro/types';

type Props = HTMLAttributes<'a'> & HTMLAttributes<'button'> & { variant?: 'primary' | 'ghost' };

const { href, variant = 'primary', class: className, ...rest } = Astro.props;
const Tag = href ? 'a' : 'button';
---
<Tag href={href} class:list={['btn', `btn-${variant}`, className]} type={href ? undefined : 'button'} {...rest}>
  <slot />
</Tag>

<Button href="/pricing/">Pricing</Button> renders an <a>; <Button>Save</Button> renders a <button type="button">. Attributes set to undefined are left out of the HTML, so the link doesn’t get a stray type.

Heading levels from a prop

Section components often need a different heading level depending on where they’re used:

---
// src/components/Section.astro
interface Props { title: string; level?: 2 | 3 | 4 }
const { title, level = 2 } = Astro.props;
const Heading = `h${level}` as 'h2' | 'h3' | 'h4';
---
<section>
  <Heading class="section-title">{title}</Heading>
  <slot />
</section>

Keeping the level a prop keeps the document outline correct, which helps screen reader users and search engines.

Rendering blocks from a CMS

Page builders and headless CMSs return a list of typed blocks. Map each type to a component:

---
// src/components/Blocks.astro
import Hero from './blocks/Hero.astro';
import Features from './blocks/Features.astro';
import Quote from './blocks/Quote.astro';
import Cta from './blocks/Cta.astro';

const components = { hero: Hero, features: Features, quote: Quote, cta: Cta } as const;

interface Props { blocks: Array<{ type: keyof typeof components; [key: string]: unknown }> }
const { blocks } = Astro.props;
---
{blocks.map(({ type, ...props }) => {
  const Block = components[type];
  return Block ? <Block {...props} /> : null;
})}

Unknown block types render nothing instead of crashing the build. You could also log them in development so editors know a block isn’t supported yet.

The hydration limitation

Client directives need Astro to know, at compile time, which file a component comes from, so it can bundle its JavaScript. A dynamic variable hides that, so <Block client:visible /> won’t hydrate. If a block needs interactivity, import it directly and branch:

---
import Carousel from './blocks/Carousel.tsx';
---
{blocks.map(({ type, ...props }) => {
  if (type === 'carousel') return <Carousel client:visible {...props} />;
  const Block = components[type];
  return Block ? <Block {...props} /> : null;
})}

Or wrap the island in a small .astro component that applies the directive inside, and put that wrapper in the map.

Common mistakes

  • Lowercase variable. const tag = 'a'; <tag> outputs a literal <tag> element. Name it Tag.
  • Unsafe strings. Don’t build a tag name from user input; restrict it with a union type as above.
  • Spreading everything. Destructure the props you use and spread the rest, so internal props like variant don’t end up as HTML attributes.

The Astro components guide covers props and slots, and typing props with TypeScript explains HTMLAttributes. For a styling helper used above, see class:list in Astro. The pages docs show how components are used in a Komal theme, and our Astro themes use this button pattern throughout.

FAQ

How do I render a component from a variable in Astro?

Assign the imported component to a capitalised variable and use it as a tag: const Component = Hero; <Component />. For many types, use an object that maps names to components.

Can I use client:load on a dynamic component in Astro?

No. Hydration directives aren’t supported on dynamic component variables, because Astro can’t tell which file to bundle. Import the island directly, or wrap it in an Astro component that applies the directive.

Why does my dynamic tag render as a literal element?

The variable starts with a lowercase letter. Astro treats lowercase tags as HTML elements. Rename it, for example from tag to Tag.