Mam komponent <Button>
.
Jeśli komponent nie ma this.props.children
, chcę ustawić prop ariaLabel
jako isRequired
, w przeciwnym razie może być opcjonalne. Jak mogę to zrobić?React rekwizyty - zestaw isRequired na podpórce, jeśli inny podpór jest pusty/pusty
ariaLabel
prop nie wymagane:
<Button>Add to bag</Button>
ariaLabel
prop musi być wymagane:
<Button ariaLabel="Add to bag" icon={ favorite } />
jeśli this.props.children
i this.props.ariaLabel
są puste, to zgłasza błąd mówiąc, że this.props.ariaLabel
jest 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 */
};
Dzięki
Trzeba będzie coś podobnego https://github.com/evcohen/react-proptype-conditional-require to zrobić w propTypes sprzeciwu. – Joe