我有一个组件<Button>
。
如果组件没有this.props.children
,我想设置道具ariaLabel
为isRequired
,否则可以是可选的。我怎么做?不需要反应道具 - 如果另一个道具为空/空,则需要一个道具
ariaLabel
道具:
<Button>Add to bag</Button>
ariaLabel
道具,必须要求:
<Button ariaLabel="Add to bag" icon={ favorite } />
如果this.props.children
和this.props.ariaLabel
是空的,它抛出一个错误,说this.props.ariaLabel
是isRequired
<Button icon={ favorite } />
propTypes:
Button.propTypes = {
/** icon inside Button. */
icon: React.PropTypes.object,
/** Content inside button */
children: React.PropTypes.node,
/** Aria-label to screen readers */
ariaLabel: React.PropTypes.string, /*isRequired if children is empty */
};
感谢
你需要像https://github.com/evcohen/react-proptype-conditional-require做的propTypes对象。 – Joe