Maaz Zindani

Loading the index…

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

React was asked to render something that is not a component (it got undefined)

ErrorError: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

Short answer

React received something that is not a tag name or a component, most often undefined, because the import resolved to nothing. The usual causes are a default import of a named export (or the reverse), a circular import that has not finished loading, a lazy module without a default export, or a Server Component using a static member such as Menu.Item of a client component.

  • Applies to React 18, React 19, Next.js 15 and 16 (App Router), Vite 6 and 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The message ends with the name of the component whose render produced the bad element, and in development the stack shows the file. Open that component, look at every capitalised tag in its JSX, and print what each import really is. A tag that prints undefined is the culprit; then the causes below say why that import came back empty.

    // in the file named by 'Check the render method of ...', next to the imports
    import * as Buttons from './Button'
    import Card from './Card'
    console.log('Card is', typeof Card, '| Button module exports:', Object.keys(Buttons))
  2. 2Match what you see, then jump to its fix

    1. 1The log shows typeof undefined for the import, and the module's export list contains the component under a name, or only default.A default import of a named export, or a named import of a default exportFix
    2. 2The same import works in one entry file and fails in another, or in tests but not in the app, and two files (or a barrel index) import each other directly or through a third.A circular import: the module you import has not finished running yetFix
    3. 3The component is loaded with lazy(() => import('./Chart')), and ./Chart exports it by name.React.lazy loads a module that has no default exportFix
    4. 4A compound component such as Menu.Item or Tabs.Panel works inside a file marked 'use client', but renders this error when a Server Component uses it. The App Router is in use.In Next.js, a Server Component uses a static member of a Client ComponentFix

What does the error look like?

  • Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
  • You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
  • Check the render method of `Dashboard`.

Cause 1 of 4

A default import of a named export, or a named import of a default export

1Check

The React docs state the rule: a file has at most one default export, the default import can take any local name, and a named import must use the same name with curly braces. Mixing them gives you undefined instead of the component. The namespace import in the first step lists exactly what the file exports.

grep -nE "export (default|const|function|\{)" src/components/Button.jsx
grep -rn "from './Button'" src

2Fix

Make the import match the export, or change the export. Pick one style per project so the mistake cannot happen silently, and avoid unnamed defaults, which also make debugging harder.

// Button.jsx
export function Button() { /* ... */ }        // named export

// correct
import { Button } from './Button'
// wrong: gives undefined, because there is no default export
import Button from './Button'

// default export version
export default function Button() { /* ... */ }
import Button from './Button'

3Verify

The log now prints function for the import and the page renders.

console.log(typeof Button)   // 'function'

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

A circular import: the module you import has not finished running yet

1Check

A cycle arises when module a imports b and b depends on a. The MDN guide explains that when one module runs inside a cycle, the other's const, let and class bindings are not initialised yet, and reading them at that moment fails; only function declarations survive. A barrel file (index.js that re-exports everything) is a common way to close a cycle. Find cycles with a tool.

npx madge --circular --extensions js,jsx,ts,tsx src

2Fix

Break the cycle, using the techniques MDN lists: move the shared code into a third module, merge the two modules, or move code from one module to the other. Components should import siblings from their own file, not from the barrel that re-exports them.

// before: Card.jsx and index.js import each other
import { Button } from './index'

// after: import the file that defines it
import { Button } from './Button'

3Verify

The cycle report is empty and the import resolves in every entry point.

npx madge --circular --extensions js,jsx,ts,tsx src   # prints no circular dependencies

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

React.lazy loads a module that has no default export

1Check

The React docs require the loader to resolve to an object whose default property is a valid component. A file with only named exports resolves to an object without that property.

grep -rn "lazy(" src
grep -n "export" src/components/Chart.jsx

2Fix

Export the component as the default, or map the named export to default inside the loader.

const Chart = lazy(() =>
  import('./Chart').then((m) => ({ default: m.Chart }))
)

3Verify

The component renders after its chunk loads, with no error boundary firing.

// open the page, watch the network tab load the chunk, and check the console is clean

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

In Next.js, a Server Component uses a static member of a Client Component

1Check

Next.js documents this case: a Server Component that imports a Client Component receives a client reference, not the function, so a property attached to the component is undefined and React throws this error. Check whether the failing tag is a dotted member and where the 'use client' line is.

grep -rnE "<[A-Z][A-Za-z]*\.[A-Z]" app components
grep -rln "^.use client" app components

2Fix

Use the compound component from another Client Component, or, to use its pieces from a Server Component, export each piece by name from the client file and import them by name.

// components/menu.tsx
'use client'
export function Menu({ children }: { children: React.ReactNode }) { /* ... */ }
export function MenuItem({ children }: { children: React.ReactNode }) { /* ... */ }

// app/page.tsx (Server Component)
import { Menu, MenuItem } from '@/components/menu'
export default function Page() {
  return <Menu><MenuItem>Settings</MenuItem></Menu>
}

3Verify

The page renders in the browser and the terminal shows no error for that route.

npm run build

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Use one export style for components (named everywhere, or default everywhere) and enforce it in review.
  • Keep barrel files out of component folders that import each other, or lint for cycles in CI.
  • Give lazy-loaded components a default export, or write the .then mapping next to the lazy call.
  • In the App Router, export compound-component parts by name instead of attaching them as static properties.

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

Web apps

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Next step

Want this handled for good?

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