React PropTypes:数字范围
React PropTypes: range of numbers
是否有更好的方法来验证数字是否在范围内?
避免写
PropTypes.oneOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
根据documentation,你可以定义你的customProps
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}
因此对于您的情况,您可以尝试以下方法
function withinTen(props, propName, componentName) {
componentName = comopnentName || 'ANONYMOUS';
if (props[propName]) {
let value = props[propName];
if (typeof value === 'number') {
return (value >= 1 && value <= 10) ? null : new Error(propName + ' in ' + componentName + " is not within 1 to 10");
}
}
// assume all ok
return null;
}
something.propTypes = {
number: withinTen,
content: React.PropTypes.node.isRequired
}
您可以使用自定义 Prop 验证器。
completed: function(props, propName, componentName) {
if (props[propName]>=1 && props[propName]<=10) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}
请参阅文档了解更多详情。
如果是sequence,可以用smart ES6。 :)
[顺便说一句,我相信第一个回答是最欣赏的,这个只是trick]
PropTypes.oneOf([...(new Array(10))].map((_, i) => i + 1))
**解释:**
这个 `[...(new Array(10))].map((_, i) => i + 1)` 部分将给出序列。
// Sequence
console.log([...(new Array(5))].map((_, i) => i + 1))
// Spreading the Sequence numbers
console.log(...[...(new Array(5))].map((_, i) => i + 1))
您可以使用支持 range
PropType
的 airbnb's extension to proptypes
range: provide a min, and a max, and the prop must be an integer in the range [min, max)
foo: range(-1, 2)
是否有更好的方法来验证数字是否在范围内?
避免写
PropTypes.oneOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
根据documentation,你可以定义你的customProps
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}
因此对于您的情况,您可以尝试以下方法
function withinTen(props, propName, componentName) {
componentName = comopnentName || 'ANONYMOUS';
if (props[propName]) {
let value = props[propName];
if (typeof value === 'number') {
return (value >= 1 && value <= 10) ? null : new Error(propName + ' in ' + componentName + " is not within 1 to 10");
}
}
// assume all ok
return null;
}
something.propTypes = {
number: withinTen,
content: React.PropTypes.node.isRequired
}
您可以使用自定义 Prop 验证器。
completed: function(props, propName, componentName) {
if (props[propName]>=1 && props[propName]<=10) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
}
请参阅文档了解更多详情。
如果是sequence,可以用smart ES6。 :)
[顺便说一句,我相信第一个回答是最欣赏的,这个只是trick]
PropTypes.oneOf([...(new Array(10))].map((_, i) => i + 1))
**解释:** 这个 `[...(new Array(10))].map((_, i) => i + 1)` 部分将给出序列。
// Sequence
console.log([...(new Array(5))].map((_, i) => i + 1))
// Spreading the Sequence numbers
console.log(...[...(new Array(5))].map((_, i) => i + 1))
您可以使用支持 range
PropType
range: provide a min, and a max, and the prop must be an integer in the range [min, max) foo: range(-1, 2)