1.) What is the difference between JSX.Element, ReactNode and ReactElement?
ReactElement and JSX.Element
are the result of invoking React.createElement
directly or via JSX transpilation. It is an object with type
, props
and key
. JSX.Element
is ReactElement
, whose props
and type
have type any
, so they are more or less the same.
const jsx = <div>hello</div>
const ele = React.createElement("div", null, "hello");
ReactNode is used as return type for render()
in class components. It also is the default type for children
attribute with PropsWithChildren
.
const Comp: FunctionComponent = props => <div>{props.children}</div>
// children?: React.ReactNode
It looks more complicated in the React type declarations, but is equivalent to:
type ReactNode = {} | null | undefined;
// super type `{}` has absorbed *all* other types, which are sub types of `{}`
// so it is a very "broad" type (I don't want to say useless...)
You can assign almost everything to ReactNode
. I usually would prefer stronger types, but there might be some valid cases to use it.
2.) Why do the render methods of class components return ReactNode, but function components return ReactElement?
tl;dr: It is a current TS type incompatibility not related to core React.
TS class component: returns ReactNode
with render()
, more permissive than React/JS
TS function component: returns JSX.Element | null
, more restrictive than React/JS
In principle, render()
in React/JS class components supports the same return types as a function component. With regard to TS, the different types are a type inconsistency still kept due to historical reasons and the need for backwards-compatibility.
Ideally a valid return type would probably look more like this:
type ComponentReturnType = ReactElement | Array<ComponentReturnType> | string | number
| boolean | null // Note: undefined is invalid
3.) How do I solve this with respect to null?
// Use type inference; inferred return type is `JSX.Element | null`
const MyComp1 = ({ condition }: { condition: boolean }) =>
condition ? <div>Hello</div> : null
// Use explicit function return types; Add `null`, if needed
const MyComp2 = (): JSX.Element => <div>Hello</div>;
const MyComp3 = (): React.ReactElement => <div>Hello</div>;
// Option 3 is equivalent to 2 + we don't need to use a global (JSX namespace)
// Use built-in `FunctionComponent` or `FC` type
const MyComp4: React.FC<MyProps> = () => <div>Hello</div>;
Note: Avoiding React.FC
won't save you from the JSX.Element | null
return type restriction.
Create React App recently dropped React.FC
from its template, as it has some quirks like an implicit {children?: ReactNode}
type definition. So using React.FC
sparingly might be preferable.
const MyCompFragment: FunctionComponent = () => <>"Hello"</>
const MyCompCast: FunctionComponent = () => "Hello" as any
// alternative to `as any`: `as unknown as JSX.Element | null`
class Example extends Component<ExampleProps> {
for classes, andconst Example: FunctionComponent<ExampleProps> = (props) => {
for function components (whereExampleProps
is an interface for the expected props). And then these types have enough information that the return type can be inferred. – Nicholas Tower