Is it possible to use React.PropTypes
to enforce length\'s on an array?
Here is a very simple case:
const TWO_NUMBERS = PropTypes.array; // i
A custom function would be the correct approach here.
const propTypes = {
TWO_NUMBERS: arrayOfLength.bind(null, 2)
}
const arrayOfLength = (expectedLength, props, propName, componentName) => {
const arrayPropLength = props[propName].length
if (arrayPropLength !== expectedLength) {
return new Error(
`Invalid array length ${arrayPropLength} (expected ${expectedLength}) for prop ${propName} supplied to ${componentName}. Validation failed.`
)
}
}