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

标签 javascript node.js reactjs next.js react-component

使用 Nextjs,我在/pages 目录中创建了一个 index.js,并在/components/meta/目录中创建了 meta.js。

当我的应用程序重建时,我收到以下错误:

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



如下所示,我正在正确导入 Meta,它也是默认导出。好奇我哪里错了。

页面/index.js
// import Head from 'next/head'
import Meta from '../components/meta/meta';

export default () => (
  <div>
    <Meta />
    <p>Hello world! Welcome to</p>
    <h1>Moonholdings.io</h1>
  </div>
)

组件/meta/meta.js
import Head from 'next/head'

export default () => (
  <Head>
    <title>Moonholdings.io</title>
    <meta name="description" content="Your Cryptocurrency Portfolio" />>
    <meta name="keywords" content="cryptocurrency, crypto, portfolio, bitcoin, ethereum, holdings"/>
    <meta name="robots" content="index, follow" />
    <meta name="viewport" content="initial-scale=1.0, width=device-width" />
  </Head>
)

项目结构

enter image description here

最佳答案

我遇到了同样的错误,因为 IDE 的自动导入功能只能导入 Head来自 next/document 的组件.
不要:

import { Head } from 'next/document';
这个Head组件将用于custom documents .
做:
import Head from 'next/head'
这个Head组件将用于您的pages .
为什么我的 IDE 没有导入正确的 Head 组件?
这不是你的 IDE 的错。嗯,不完全是……next/head组件使用默认导出。默认导出不能很好地与自动导入配合使用。这就是为什么默认导出被认为是不好的做法的原因之一。非常不幸的是,Vercel 选择允许他们参与他们的项目。

关于javascript - NextJS : Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981805/

相关文章:

javascript - 如何在数组中找到包含最大值的对象

javascript - 提交后重定向到另一个页面

javascript - 当我单击链接时,jQuery 表行单击事件也会触发

node.js - 如果 Github Actions 中未指定,则使用什么 Node 版本

node.js - 在 Node.JS 中使用异步/等待的正确请求

javascript - 如何将样式类应用于按钮,但在 Vue 中任何时候只能激活一个按钮?

node.js - Joi验证: minimum length function is not working

javascript - 编译失败: Cannot find file './containers/App/App' in './src'

reactjs - 如何在reactjs中选择要复制的文本而不触发点击事件

javascript - React - Forms - 如何处理子组件更新