首页 > 其他 > 详细

PropTypes.element和PropTypes.node的区别

时间:2019-10-17 09:11:38      阅读:274      评论:0      收藏:0      [点我收藏+]

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

 

PropTypes.element和PropTypes.node的区别

原文:https://www.cnblogs.com/94pm/p/11688918.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!