如何检查React组件是否期望支柱?

时间:2017-06-28 05:03:17

标签: reactjs

我试图在Modal中包含各种组件,这会将closeModal属性传递给任何支持它的子项。

目前我跟随this post并使用JS属性closesModal来表示父母应该传递更接近的方法,即:

React.Children.map(
  this.props.children,
  child => (
    child.closesModal ?
      React.cloneElement(child, { closeModal: this.close }) :
      child
  )
)

这是一种公平的方式来实现我想要的东西吗?有没有办法反省React组件以查看它是否需要一个属性?

1 个答案:

答案 0 :(得分:3)

以下是相同的示例:

101
010
101

文档:https://facebook.github.io/react/docs/typechecking-with-proptypes.html