簡體   English   中英

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'

我嘗試了刪除node_modules/yarn.lock並重新安裝所有內容的推薦解決方案,但沒有解決。

我正在制作一個簡單的路由器,它根據道具呈現子級:

import React, { Fragment } from "react";

type RouterProps = {
  currentRoute: string;
  children: React.ReactNode;
};

const Router = ({ currentRoute, children }: RouterProps) => {
  return React.Children.map(children, child =>
    React.cloneElement(child as React.ReactElement<any>, { currentRoute })
  );
};

type RouterViewProps = {
  route: string;
  children: any;
};

Router.View = ({ route, currentRoute, children }: RouterViewProps) => (
  <div>{currentRoute === route ? <Fragment>{children}</Fragment> : null}</div>
);

export default Router;

嘗試在應用程序中使用我的組件時出現錯誤:

import React from "react";
import Router from "./components/Router";
import Home from "./components/Home";

function App() {
  return (
    <div>
      <Router currentRoute="home">
        <Router.View route="home">
          <Home />
        </Router.View>
      </Router>
    </div>
  );
}

export default App;

完整錯誤:

TypeScript error in /Users/gonzo/Projects/JS/filex-workshops-registration/src/App.tsx(8,7):
JSX element 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 | undefined' is not a constructor function for JSX elements.
  Type 'undefined' is not assignable to type 'Element | null'.  TS2605

     6 |   return (
     7 |     <div>
  >  8 |       <Router currentRoute="home">
       |       ^
     9 |         <Router.View route="home">
    10 |           <Home />
    11 |         </Router.View>

Router 組件在我的測試中完美運行,所以我不明白應用程序本身有什么不同。

Router不是 JSX 的構造函數,因為它不返回 JSX。

const Router = ({ currentRoute, children }: RouterProps) => {
  return (
    <>
       {React.Children.map(children, child =>
          React.cloneElement(child as React.ReactElement<any>, { currentRoute })
       )}
    </>
  );
};

輸入'元素| 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 元素類型“字符串”不是 JSX 元素的構造函數 function。 Typescript Ionic React JSX 元素類型 'false | Element' 不是 JSX 元素的構造函數 function。 輸入'假 打字稿錯誤 JSX 元素類型“視圖”不是 JSX 元素的構造函數 JSX元素類型&#39;Line&#39;不是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,> 類型 '() =&gt; JSX.Element' 不可分配給類型 'ReactNode' “組件”不能用作 JSX 組件。 它的元素類型&#39;ReactElement<any, any> | Component&lt;{}, any, any&gt;&#39; 不是有效的 JSX 元素 Typescript:類型“Element[]”不可分配給類型“ReactElement” <any, string | jsxelementconstructor<any> >'</any,> 在 TypeScript 中使用 JS React 組件:JSX 元素類型“MyComponent”不是 JSX 元素的構造函數 “Element”類型的參數不可分配給“ReactElement”類型的參數<any></any>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM