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:
'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.
Why is ReactNode
not valid JSX and how can I solve this issue?
Edit:
I know that wrapping value
in React fragments solves the issue. However, in this specific application I need the type DatetimeCell
to be able to return any valid JSX. So string
should be included.
Why is ReactNode not valid JSX ?? => https://stackoverflow.com/a/72353143/10146901
and how can I solve this issue?
=> Just Return JSX.Element
as function return type.
function buildCell({ value }: { value: string }): JSX.Element { // ... my codes }
Solution of your codesandbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With