简体   繁体   English

“ReactNode”不是有效的 JSX 元素

[英]'ReactNode' is not a valid JSX element

I have the following piece of code ( codesandbox ):我有以下代码(代码和框):

import { ComponentType, ReactNode } from "react";

type DatetimeCell = ({ value }: { value: string }) => ReactNode;

function getDateTimeCell(): DatetimeCell {
  return ({ value }) => value;
}

function buildCell({
  value
}: {
  value: string;
}): ComponentType<{ value: string }> {
  const DateTimeCell = getDateTimeCell();
  return ({ value }) => <DateTimeCell value={value} />;
}

When returning in buildCell I get the error:buildCell中返回时出现错误:

'DateTimeCell' cannot be used as a JSX component.
  Its return type 'ReactNode' is not a valid JSX element.

I thought ReactNode would be the most general type for valid JSX, but it seems like that is not the case.我认为ReactNode将是有效 JSX 的最通用类型,但似乎情况并非如此。
Why is ReactNode not valid JSX and how can I solve this issue?为什么ReactNode不是有效的 JSX,我该如何解决这个问题?

Edit: I know that wrapping value in React fragments solves the issue.编辑:我知道在 React 片段中包装value可以解决问题。 However, in this specific application I need the type DatetimeCell to be able to return any valid JSX.但是,在这个特定的应用程序中,我需要DatetimeCell类型才能返回任何有效的 JSX。 So string should be included.所以应该包括string

Why is ReactNode not valid JSX ??为什么 ReactNode 不是有效的 JSX ? => https://stackoverflow.com/a/72353143/10146901 => https://stackoverflow.com/a/72353143/10146901

and how can I solve this issue?我该如何解决这个问题? => Just Return JSX.Element as function return type. => 只需JSX.Element作为函数返回类型返回。

function buildCell({ value }: { value: string }): JSX.Element { // ... my codes }

Solution of your codesandbox您的密码箱的解决方案

Part of ReactNode type is undefined , which is not a valid JSX element I think. ReactNode类型的一部分是undefined ,我认为这不是一个有效的 JSX 元素。

The easiest way to solve the problem would be just to type DatetimeCell as Component as well, and always return an element from getDateTimeCell - just wrap the result in a fragment.解决问题的最简单方法是将DatetimeCell也键入为 Component,并始终从getDateTimeCell返回一个元素 - 只需将结果包装在一个片段中。

import React, { ComponentType } from 'react';

type DatetimeCell = ComponentType<{ value: string }>;

function getDateTimeCell(): DatetimeCell {
  return ({ value }) => <>{value}</>;
}

function buildCell({ value }: { value: string }): ComponentType<{ value: string }> {
  const DateTimeCell = getDateTimeCell();
  return ({ value }) => <DateTimeCell value={value} />;
}

When you invoke DateTimeCell it expects a JSX element (ReactNode) to be returned.当您调用 DateTimeCell 时,它期望返回一个 JSX 元素 (ReactNode)。 You are currently returning a string and not a React node in your getTimeCell function.您当前在 getTimeCell 函数中返回一个字符串而不是 React 节点。 I recommend typing your functional components using the FC type provided by react as below:我建议使用 react 提供的 FC 类型键入您的功能组件,如下所示:

import { FC } from "react";

type Props = { value: string };
type DatetimeCell = FC<Props>;

function getDateTimeCell(): DatetimeCell {
  return ({ value }) => <>{value}</>;
}

function buildCell({ value }: Props): DatetimeCell {
  const DateTimeCell = getDateTimeCell();
  return ({ value }) => <DateTimeCell value={value} />;
}

buildCell({ value: "hello" });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM