Is there a way to get the keys for any propTypes referencing an optional property (i.e. not specified as mandatory)?
For example, given the following details:
TestComponent.propTypes = { requiredProp: PropTypes.string.isRequired, optionalProp: PropTypes.func, optionalProp2: PropTypes.element }
... I can get an array containing the elements: ["optionalProp", "optionalProp2"]
If there is no built-in way to do this, is there an elegant solution that:
- Avoids hard-coding a list of them
- Prevents getting a custom class using this functionality
- It can be used in all of my reacting components.
I thought to use a context to define such a function, and then call it the current component as follows: this.context.getOptionalProps.call(this)
But this seems like a poor use of context.
source share