簡體   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

  • 我在存儲庫上運行“yarn build”和/或“npm run build”。 (兩個命令的錯誤相同)
  • 我正在嘗試構建此代碼並將其部署到 AWS 環境中,所以現在我只是試圖克服告訴我“編譯失敗”的錯誤
  • 該錯誤僅指向整個應用程序中的一個點。 這一行:getLayout(<Component {...pageProps} />)

這是完整的錯誤;

類型錯誤:“組件”不能用作 JSX 組件。 它的元素類型 'ReactElement<any, any> | Component<{}, any, any>' 不是有效的 JSX 元素。

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

錯誤命令失敗,退出代碼為 1。

我已經閱讀了關於“組件不能用作 JSX 組件”的類似問題和帖子,但它們並沒有幫助我繼續前進。

版本:react、@types/react、@types/react-dom,均設置為版本 17.0.2

我已經刪除了 package-lock.json 文件,現在只留下了 yarn-lock.json 文件

我缺少什么線索來解決這個難題?

親切的問候

問題在於最新版本的 @types/react:18.xx 這可能是通過其他一些包的對等依賴項引入您的項目的。

如果您有 .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) RC-dock 庫的 &#39;DockLayout&#39; 不能用作 JSX 組件。 它的實例類型“DockLayout”不是有效的 JSX 元素 'Provider' 不能用作 JSX 組件。 它的實例類型'Provider<anyaction> ' 不是有效的 JSX 元素。 TS2786</anyaction> JSX 元素類型 &#39;ReactElement<any> 不是 JSX 元素的構造函數。 類型 &#39;undefined&#39; 不能分配給類型 &#39;Element | 空值&#39; “組件”不能用作 JSX 組件。 下一步 TypeScript 3:JSX 元素類型“組件”沒有任何構造或調用簽名。 [2604] 組件不能用作 JSX 組件 輸入'元素| 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,> JSX 元素類型“x”沒有任何構造或調用簽名 有沒有辦法在 React 中定義一個可以在整個組件中使用的元素?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM