PropTypes.element和PropTypes.node的区别

PropTypes.element:指React Element,即React.CreateElement生成的元素,React.CreateElement可以用jsx语法糖表示:

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

编译后为:

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

因此PropTypes.element可以为以下类型:string | 组件实列(组件标签,例如上面的<MyButtom>)

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

摘自大神的回答:

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

猜你喜欢

转载自www.cnblogs.com/94pm/p/11688918.html
今日推荐