Should a component's render method have return type React.ReactNode or JSX.Element?

后端 未结 2 1402
慢半拍i
慢半拍i 2021-01-03 23:40

Both seem to result in no compilation errors, but what\'s the difference and is one preferable to the other?

2条回答
  •  温柔的废话
    2021-01-04 00:20

    It depends. ReactJS in principle can render:

    type RenderType = JSX.Element* | Array | string | number | boolean | null
    // * includes Portal, Fragment
    // for both function and class components
    // (type doesn't exist in React type declarations)
    

    TS render types are currently limited to:

    • Class component: ReactNode (wider than permitted by React)

    • Function component: JSX.Element | null (more restrictive than React)

    JSX.Element is more or less the same as ReactElement, you can use both interchangeably.

提交回复
热议问题