The difference PropTypes.element and PropTypes.node

PropTypes.element: means React Element, i.e. React.CreateElement generated elements, React.CreateElement jsx syntactic sugar can be represented by:

<MyButton color="blue" shadowSize={2}>
  Click Me
</MyButton>

Compiled as follows:

React.createElement(
  MyButton,
  {color: 'blue', shadowSize: 2},
  'Click Me'
)

Thus PropTypes.element may be of the following types: string | column solid component (component tag, for example above <MyButtom>)

 

PropTypes.node: finger React Node,任何可被渲染的元素,包括ReactChild | ReactFragment | ReactPortal | 字符串 | 数字 | 布尔值 | null | undefined | 数组;

 

Excerpt from the god's answer:

Quote @ferdaber: A more technical explanation is that a valid React node is not the same thing as what is returned by React.createElement. Regardless of what a component ends up rendering, React.createElement always returns an object, which is the JSX.Element interface, but React.ReactNode is the set of all possible return values of a component.

  • JSX.Element -> Return value of React.createElement
  • React.ReactNode -> Return value of a component

 

Guess you like

Origin www.cnblogs.com/94pm/p/11688918.html