简体   繁体   English

“组件”不能用作 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

  • I am running "yarn build" and/or "npm run build" on a repository.我在存储库上运行“yarn build”和/或“npm run build”。 (same error for both commands) (两个命令的错误相同)
  • I am trying to build and deploy this code to an AWS environment so right now I am simply trying to get past the errors that tell me "failed to compile"我正在尝试构建此代码并将其部署到 AWS 环境中,所以现在我只是试图克服告诉我“编译失败”的错误
  • The error is pointing to only one point in the whole app.该错误仅指向整个应用程序中的一个点。 This line : getLayout(<Component {...pageProps} />)这一行:getLayout(<Component {...pageProps} />)

Here is the full error;这是完整的错误;

Type error: 'Component' cannot be used as a JSX component.类型错误:“组件”不能用作 JSX 组件。 Its element type 'ReactElement<any, any> |它的元素类型 'ReactElement<any, any> | Component<{}, any, any>' is not a valid JSX element. Component<{}, any, any>' 不是有效的 JSX 元素。

                     <Loader />
                   ) : (
                     getLayout(<Component {...pageProps} />)
                                ^
                   )
                 }
               </AuthConsumer>

error Command failed with exit code 1.错误命令失败,退出代码为 1。

I have read similar questions and posts about this "component cannot be used as a JSX component" and they have not helped me on my quest to move forward.我已经阅读了关于“组件不能用作 JSX 组件”的类似问题和帖子,但它们并没有帮助我继续前进。

Versions: react, @types/react, @types/react-dom, all set to version 17.0.2版本:react、@types/react、@types/react-dom,均设置为版本 17.0.2

I have deleted the package-lock.json files and left only the yarn-lock.json files for now我已经删除了 package-lock.json 文件,现在只留下了 yarn-lock.json 文件

What clue am I missing to solve this puzzle?我缺少什么线索来解决这个难题?

kind regards亲切的问候

The Issue is with Latest Version of @types/react:18.xx This may have been introduced to your project via some other packages' peer dependency.问题在于最新版本的 @types/react:18.xx 这可能是通过其他一些包的对等依赖项引入您的项目的。

If you have a .tsconfig file, Add following to compilerOptions:如果您有 .tsconfig 文件,请将以下内容添加到 compilerOptions:

"paths": {
      "react": [ "./node_modules/@types/react" ]
    },

输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> - Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any)

暂无
暂无

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

相关问题 不能用作 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) 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 '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 JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; - JSX element type 'ReactElement<any> is not a constructor function for JSX elements. Type 'undefined' is not assignable to type 'Element | null' “组件”不能用作 JSX 组件。 下一步 - 'Component' cannot be used as a JSX component. Nextjs TypeScript 3:JSX 元素类型“组件”没有任何构造或调用签名。 [2604] - TypeScript 3: JSX element type 'Component' does not have any construct or call signatures. [2604] 组件不能用作 JSX 组件 - Component cannot be used as a JSX component 输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> - Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) JSX 元素类型“x”没有任何构造或调用签名 - JSX element type 'x' does not have any construct or call signatures 有没有办法在 React 中定义一个可以在整个组件中使用的元素? - Is there any way to define an element in React that can be used throughout the component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM