How to solve this error: "Alert" cannot be used as a jsx component. Its return type 'void' is not a valid JSX element

huangapple go评论61阅读模式
英文:

How to solve this error: "Alert" cannot be used as a jsx component. Its return type 'void' is not a valid JSX element

问题

App.tsx

import { Alert } from "./Alert";
import "./styles.css";

function App() {
  return (
    <>
      <div className="App">
        <Alert heading="Success">Everything is really good!</Alert>
      </div>
    </>
  );
}
export default App;

Alert.tsx

type Props = {
  type?: string;
  heading: string;
  children: React.ReactNode;
  closable?: boolean;
  onClose?: () => void;
};

export function Alert({
  type = "information",
  heading,
  children,
  closable,
  onClose,
}: Props) {}

Error: "Alert" 无法用作 JSX 组件。其返回类型 'void' 不是有效的 JSX 元素。

英文:

App.tsx

import { Alert } from &quot;./Alert&quot;;
import &quot;./styles.css&quot;;

function App() {
  return (
    &lt;&gt;
      &lt;div className=&quot;App&quot;&gt;
        &lt;Alert heading=&quot;Success&quot;&gt;Everything is really good!&lt;/Alert&gt;
      &lt;/div&gt;
    &lt;/&gt;
  );
}
export default App;

Alert.tsx

type Props = {
  type?: string;
  heading: string;
  children: React.ReactNode;
  closable?: boolean;
  onClose?: () =&gt; void;
};

export function Alert({
  type = &quot;information&quot;,
  heading,
  children,
  closable,
  onClose,
}: Props) {}

I tried to update all packages nothing helps.
Error: ""Alert" cannot be used as a jsx component. Its return type 'void' is not a valid JSX element"

答案1

得分: 1

没有在那里返回任何JSX元素,所以导致了问题。
<br />
在Alert.tsx中尝试这样做:

type Props = {
  type?: string;
  heading: string;
  children: React.ReactNode;
  closable?: boolean;
  onClose?: () => void;
};

export function Alert({
  type = "information",
  heading,
  children,
  closable,
  onClose
}: Props) {
  return <div>{children}</div>;
}
英文:

You didn't return any JSX Element there so it caused the issue.
<br />
Try this in Alert.tsx:

type Props = {
  type?: string;
  heading: string;
  children: React.ReactNode;
  closable?: boolean;
  onClose?: () =&gt; void;
};

export function Alert({
  type = &quot;information&quot;,
  heading,
  children,
  closable,
  onClose
}: Props) {
  return &lt;div&gt;{children}&lt;/div&gt;;
}

huangapple
  • 本文由 发表于 2023年6月5日 23:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定