![](/img/trans.png)
[英]Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS
[英]Recoil Nexus - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got object
我开始收到错误:
error - Error: Element type is invalid: expected a string (for built-in components) or
a class/function (for composite components) but got: object.
at ReactDOMServerRenderer.render ...
在我的 NextJS 项目中添加 TypeScript 支持时。
我搜索了类似的问题,但答案主要是关于错误的导入/导出。 但就我而言,似乎并非如此。
在此错误之前我还会收到警告:
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or
a class/function (for composite components) but got: object.
Check your code at _app.js:18.
at App (webpack-internal:///./pages/_app.js:19:16)
这是我的_app.js
:
import "../styles/global.scss";
import "video.js/dist/video-js.css";
import { RecoilRoot } from "recoil";
import RecoilNexus from "recoil-nexus";
import { CookiesProvider } from "react-cookie";
import Head from "next/head";
export default function App({ Component, pageProps }) {
return (
<>
<Head>
<meta name="theme-color" content="#fff" />
<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
<CookiesProvider>
<RecoilRoot>
<RecoilNexus />
<Component {...pageProps} />
</RecoilRoot>
</CookiesProvider>
</>
);
}
因此,由于<RecoilNexus />
部分而产生了错误。
我尝试评论它并且错误消失了。 但是,我仍然需要在我的项目中使用它。
只需将我的_app.js
文件扩展名更改为_app.tsx
解决问题。 我仍然不确定为什么会发生这种情况,以及这个问题是否仅在使用recoil-nexus
并将 TS 添加到 NextJS 项目时才存在,或者这是在将 TS 添加到现有项目时必须做的事情。 我很高兴知道更多关于这个原因的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.