【发布时间】:2022-12-20 00:12:58
【问题描述】:
这是 _app.tsx 的样子:
function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
我在构建项目时遇到此错误:
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'.
这些是反应版本:
"react": "17.0.2",
"react-dom": "17.0.2",
"@types/react": "17.0.26",
我尝试切换到 18 版本,它起作用了,但是我收到了这个错误:Hydration failed because the initial UI does not match what was rendered on the server
【问题讨论】:
-
你传递给
Component道具的是什么? -
@DaveNewton 你是说这个:
{...pageProps}? -
确保您尝试呈现的页面是有效的 React 组件。请记住
Component在此上下文中是您的页面组件,因此它表示您的页面组件不是有效的 React 组件。 -
不,我的意思是你将什么作为
Component道具传递给MyApp。 -
@DaveNewton 如果我理解正确,那么我不会将任何道具传递给
MyApp,因为 nextjs 确实传递了一些东西,我不知道是什么。这是 _app.tsx 文件
标签: javascript reactjs typescript next.js