简体   繁体   English

“组件”不能用作 JSX 组件。 下一步

[英]'Component' cannot be used as a JSX component. Nextjs

This is how _app.tsx looks:这是 _app.tsx 的样子:

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />
}

and I am getting this error while building project:我在构建项目时遇到此错误:

Type error: 'Component' cannot be used as a JSX component.
  Its element type 'ReactElement<any, any> | Component<{}, any, any> | null' is not a valid JSX element.
    Type 'Component<{}, any, any>' is not assignable to type 'Element | ElementClass | null'.
      Type 'Component<{}, any, any>' is not assignable to type 'ElementClass'.
        The types returned by 'render()' are incompatible between these types.
          Type 'React.ReactNode' is not assignable to type 'import("/Users/user/node_modules/@types/react/index").ReactNode'.
            Type '{}' is not assignable to type 'ReactNode'.

These are the react versions:这些是反应版本:

"react": "17.0.2",
"react-dom": "17.0.2",
"@types/react": "17.0.26",

I tried to switch to 18 version, it worked but, I got this error: Hydration failed because the initial UI does not match what was rendered on the server我尝试切换到 18 版本,但它有效,但我收到此错误: Hydration failed because the initial UI does not match what was rendered on the server

Add the following code into package.json file将以下代码添加到package.json文件中

"resolutions": {
  "@types/react": "17.0.2",
  "@types/react-dom": "17.0.2"
}

Then Reinstall the packages然后重新安装软件包

yarn
  • if using npm如果使用 npm
npm i

I think that is because you did not set typescript properly in your project.我认为这是因为您没有在项目中正确设置打字稿。 I think, currently, when you install next.js, you should have option to select typescript .我认为,目前,当您安装 next.js 时,您应该可以选择typescript If not如果不

npm install --save-dev typescript @types/react @types/node.

in tsconfig.json, you should have "jsx": "preserve" in compiler options.在 tsconfig.json 中,您应该在编译器选项中有"jsx": "preserve" It allows us to use .tsx files in the project.它允许我们在项目中使用 .tsx 文件。 an example of tsconfig.json tsconfig.json 的一个例子

  • I think this might be the issue.我认为这可能是问题所在。 In order to indicate that we are returning jsx we have to wrap the component with () .为了表明我们正在返回jsx ,我们必须用()包装组件。 but in your component you have但是在你的组件中你有

    return <Component {...pageProps} />

even though I tried to put () , vscode omits it.即使我尝试放置() ,vscode 也忽略了它。 So try arrow function:所以尝试箭头功能:

const App = ({ Component, pageProps }: AppProps) => (
  <Component {...pageProps} />
);

export default App;

Update React types to 18 in devDependencies (If you use already 17.x):将 devDependencies 中的 React 类型更新为 18(如果你已经使用 17.x):

"@types/react": "^18",
"@types/react-dom": "^18"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React / NextJS:类型错误:'Component' 不能用作 JSX 组件 - React / NextJS: Type error: 'Component' cannot be used as a JSX component 不能用作 JSX 组件。 它的返回类型“void”不是有效的 JSX element.ts(2786) - cannot be used as a JSX component. Its return type 'void' is not a valid JSX element.ts(2786) “组件”不能用作 JSX 组件。 它的元素类型&#39;ReactElement<any, any> | Component&lt;{}, any, any&gt;&#39; 不是有效的 JSX 元素 - 'Component' cannot be used as a JSX component. Its element type 'ReactElement<any, any> | Component<{}, any, any>' is not a valid JSX element 组件不能用作 JSX 组件 - Component cannot be used as a JSX component 'Provider' 不能用作 JSX 组件。 它的实例类型'Provider<anyaction> ' 不是有效的 JSX 元素。 TS2786</anyaction> - 'Provider' cannot be used as a JSX component. Its instance type 'Provider<AnyAction>' is not a valid JSX element. TS2786 RC-dock 库的 &#39;DockLayout&#39; 不能用作 JSX 组件。 它的实例类型“DockLayout”不是有效的 JSX 元素 - RC-dock library's 'DockLayout' cannot be used as a JSX component. Its instance type 'DockLayout' is not a valid JSX element “路由器”不能用作 JSX 组件 - 'Router' cannot be used as a JSX component react 'component' 不能用作 JSX 组件 - react 'component' cannot be used as a JSX component &#39;Router&#39; 不能用作 JSX 组件。 在我在 index.tsx 中添加组件 Msal 组件和在 app.tsx 中添加 PageLayout 之前,它工作正常 - 'Router' cannot be used as a JSX component. It was working fine until I added the component Msal componant in index.tsx and PageLayout in app.tsx &#39;Router&#39;/&#39;GlobalStyles&#39; 不能用作 JSX 组件 - 'Router'/'GlobalStyles' cannot be used as a JSX component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM