Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Astro Code component fails inside a React component

ErrorAstro components cannot be rendered directly via function call

Short answer

Astro components such as Code only exist on the server and render static HTML. You cannot import a .astro component into a React file or call it like a function. Use Code in an .astro file, and pass the result into your React component as children or a named slot.

  • Applies to Astro 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the file where Code is imported. If it ends in .jsx or .tsx, that is the problem.

    grep -rn "astro:components" src/ | grep -v "\.astro:"
  2. 2Match what you see, then jump to its fix

    1. 1The import of astro:components sits in a .jsx or .tsx fileCode is imported inside a React fileFix
    2. 2You want the highlight to appear inside a React layoutThe React component needs to show highlighted codeFix

What does the error look like?

  • Astro components cannot be rendered directly via function call

Cause 1 of 2

Code is imported inside a React file

1Check

The Astro docs say .astro components cannot be imported into a UI framework component such as .jsx or .svelte.

grep -rln "from 'astro:components'" src/

2Fix

Move the use of Code into an .astro file. The import and the props go in the frontmatter and the template.

---
import { Code } from 'astro:components';
---
<Code code={`const foo = 'bar';`} lang="js" />

3Verify

Restart the dev server. The page renders highlighted code.

npm run dev

Confirmed by docs.astro.build

Did this fix it?

Cause 2 of 2

The React component needs to show highlighted code

1Check

Astro markup can be passed to a framework component as children or a named slot, but only from an .astro file.

grep -rn "slot=" src/

2Fix

Wrap the React component in the .astro page and put Code inside it as a child.

---
import { Code } from 'astro:components';
import Card from '../components/Card.jsx';
---
<Card>
  <Code code={`npm run build`} lang="bash" />
</Card>

3Verify

The card renders and the code inside is highlighted.

npm run build

Confirmed by docs.astro.build

Did this fix it?

How do I stop it coming back?

  • Keep Astro components in .astro files and let React components take children.
  • Remember that Astro components render to static HTML and have no client-side runtime.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.