简体   繁体   English

使用自定义条件渲染组件时,类型 JSX.Element 不可分配给类型 React.ReactNode

[英]Type JSX.Element is not assignable to type React.ReactNode when using customed conditional render component

I'm using next: "12.1.6" and react: "18.2.0".我正在使用下一个:“12.1.6”并做出反应:“18.2.0”。

I have a template component for conditional rendering.我有一个用于条件渲染的模板组件。

// Show.tsx
interface Props {
  when: boolean;
  fallback?: React.ReactNode;
  children: React.ReactNode;
}

function Show(props: Props) {
  if (props.when) {
    return props.children;
  }
  return props.fallback ?? null;
}

export default Show;

and I use the template componenet like this but has an issue.我像这样使用模板组件但有问题。

<Show
  when={connectionStatus}
  fallback={
    <div className="border rounded bg-red-400 py-2 px-5">
      ❌ disconnected
    </div>
    }
  >
  <div className="border rounded bg-green-400 py-2 px-5">
    ✅ connected
  </div>
</Show>

at fallback attribute, IDE represents an error "Type JSX.Element is not assignable to type React.ReactNode".fallback属性中,IDE 表示错误“Type JSX.Element is not assignable to type React.ReactNode”。

Both React.ReactElement and JSX.Element is a result of React.createElement so I thought there is no specific difference between them except namespace. React.ReactElementJSX.Element都是React.createElement的结果,所以我认为除了命名空间之外它们之间没有特别的区别。 But it seems React.ReactNode cannot cover JSX.Element .但似乎React.ReactNode无法覆盖JSX.Element

Waht should I update or change?我应该更新或更改什么?

ReactNode is a ReactElement, a ReactFragment, a string, a number or an array of ReactNode ReactNode是一个ReactElement,一个ReactFragment,一个字符串,一个数字或者一个ReactNode的数组

But in here you are returning an HTML template which is not part of above mentioned categories.但是在这里,您返回的是不属于上述类别的 HTML 模板。 Hence you need to change the type to JSX.Element因此,您需要将类型更改为JSX.Element

fallback?: JSX.Element

Use Function to wrap fallback JSX Element,使用 Function 包装fallback JSX 元素,

like this:像这样:

<Show
  when={connectionStatus}
  fallback={() => {
    return <div className="border rounded bg-red-400 py-2 px-5">
      ❌ disconnected
    </div>
    }}
  >
  <div className="border rounded bg-green-400 py-2 px-5">
    ✅ connected
  </div>
</Show>

And update Props interface to:并将Props interface更新为:

interface Props extends React.PropsWithChildren<{
  when: boolean;
  fallback?: () => JSX.Element;
}> {}

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

相关问题 类型 '() =&gt; JSX.Element' 不可分配给类型 'ReactNode' - Type '() => JSX.Element' is not assignable to type 'ReactNode' '(props: ITableProps) =&gt; JSX.Element' 类型的参数不能分配给类型的参数...... - React HOC - Argument of type '(props: ITableProps) => JSX.Element' is not assignable to parameter of type … - React HOC React.ReactNode 反应元素 - React.ReactNode to react element 什么时候使用 JSX.Element vs ReactNode vs ReactElement? - When to use JSX.Element vs ReactNode vs ReactElement? 从react JSX.Element恢复组件引用 - Recovering Component ref from react JSX.Element “类型 '{ children: Element[]; pictureUrl: string; }' 不可分配给类型 'IntrinsicAttributes &amp; { children?: ReactNode; }' - "Type '{ children: Element[]; pictureUrl: string; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }' 将 useState 与 JSX.Element 反应 - react useState with JSX.Element 如何从React组件的render方法获取纯文本或将JSX.Element转换为字符串? - How can I get the plain text from the render method from React component or convert a JSX.Element to string? 错误:类型“void”不可分配给类型“ReactNode” - Error: Type 'void' is not assignable to type 'ReactNode' 反应 typescript 句柄 JSX.Element 或字符串 - React typescript handle JSX.Element or string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM