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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM