如何声明React PropTypes XOR

Dam*_*oux 5 reactjs react-proptypes

我曾经用多个配置编写组件例如:

ResponsiveTable.PropTypes = {
  width: React.PropTypes.number, //used if widthOffset and minWidth are undefined
  widthOffset: React.PropTypes.number, //used if width is undefined
  minWidth: React.PropTypes.number, //used if width is undefined
};
Run Code Online (Sandbox Code Playgroud)

我怎样才能声明只有在我已经设置了其他道具的情况下才能使用的道具?

一个XOR选项是有用的.我阅读https://facebook.github.io/react/docs/reusable-components.html但它没有帮助.

有任何想法吗?

Dam*_*oux 2

解决方案

React.PropTypes.oneOfType([
    React.PropTypes.shape({
        width: React.PropTypes.number.isRequired
    }),
    React.PropTypes.shape({
        widthOffset: React.PropTypes.number,
        minWidth: React.PropTypes.number.isRequired
    }),  
])
Run Code Online (Sandbox Code Playgroud)