Theodor Theodor - 23 days ago 6
React JSX Question

Get React propType name, type and isRequired

I noticed that react components have a

type.propTypes
object, which hold all propTypes as keys.

For instance,

Component.propTypes = { initialCount: React.PropTypes.number }


looks like

React component type

in Chrome dev tools. So I can get the name of the proptypes using

Object.keys(component.type.propTypes)


but can I also get the proptypes type and whether or not its required?

Answer

Not really. If you check ReactPropTypes.js the validation methods exported use an expectedType within a private scope.

Also interesting to have a look at ReactElementValidator.js to see how it calls the propType validation.

If you wanted to test against the primitive types you could probably iterate calls with fake props to the validation method and catch errors until it does not throw, but I don't think that is what you were after.