Accepted answer

what is the difference between jsx.element, reactnode and reactelement?

a reactelement is an object with a type and props.

 type key = string | number

 interface reactelement<p = any, t extends string | jsxelementconstructor<any> = string | jsxelementconstructor<any>> {
    type: t;
    props: p;
    key: key | null;

a reactnode is a reactelement, a reactfragment, a string, a number or an array of reactnodes, or null, or undefined, or a boolean:

type reacttext = string | number;
type reactchild = reactelement | reacttext;

interface reactnodearray extends array<reactnode> {}
type reactfragment = {} | reactnodearray;

type reactnode = reactchild | reactfragment | reactportal | boolean | null | undefined;

jsx.element is a reactelement, with the generic type for props and type being any. it exists, as various libraries can implement jsx in their own way, therefore jsx is a global namespace that then gets set by the library, react sets it like this:

declare global {
  namespace jsx {
    interface element extends react.reactelement<any, any> { }

by example:

 <p> // <- reactelement = jsx.element
   <custom> // <- reactelement = jsx.element
     {true && "test"} // <- reactnode

why do the render methods of class components return reactnode, but function components return reactelement?

indeed, they do return different things. components return:

 render(): reactnode;

and functions are "stateless components":

 interface statelesscomponent<p = {}> {
    (props: p & { children?: reactnode }, context?: any): reactelement | null;
    // ... doesn't matter

this is actually due to historical reasons.

how do i solve this with respect to null?

type it as reactelement | null just as react does. or let typescript infer the type.

source for the types


export declare interface appprops {
  children1: jsx.element; // bad, doesnt account for arrays
  children2: jsx.element | jsx.element[]; // meh, doesn't accept strings
  children3: react.reactchildren; // despite the name, not at all an appropriate type; it is a utility
  children4: react.reactchild[]; // better, accepts array children
  children: react.reactnode; // best, accepts everything (see edge case below)
  functionchildren: (name: string) => react.reactnode; // recommended function as a child render prop type
  style?: react.cssproperties; // to pass through style props
  onchange?: react.formeventhandler<htmlinputelement>; // form events! the generic parameter is the type of
  //  more info:
  props: props & react.componentpropswithoutref<"button">; // to impersonate all the props of a button element and explicitly not forwarding its ref
  props2: props & react.componentpropswithref<mybuttonwithforwardref>; // to impersonate all the props of mybuttonforwardedref and explicitly forwarding its ref


reactelement is the type for elements in react, either created via jsx or react.createelement.

const a = <div/> // this is a reactelement

reactnode is wider, it can be text, number, boolean, null, undefined, a portal, a reactelement, or an array of reactnodes. it represents anything that react can render.

const a = (
     hello {[1, "world", true]} // this is a reactnode

jsx.element is an internal hook for typescript. it is set equal to reactelement to tell typescript that every jsx expressions should be typed as reactelements. but if we'd use preact, or other technologies using jsx it would be set to something else.

functional components return reactelement | null, so it cannot return a bare string or an array of reactelements. it is a known limitation. the workaround is to use fragments :

const foo: fc = () => {
  return <>hello world!</>  // this works

class components' render function return reactnode, so there shouldn't be any problem.


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?

some options:
// 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.

in edge cases, you can add a type assertion or fragments as workaround:
const mycompfragment: functioncomponent = () => <>"hello"</>
const mycompcast: functioncomponent = () => "hello" as any 
// alternative to `as any`: `as unknown as jsx.element | null`

Related Query

More Query from same tag