Parses React prop-types into a readable object
npm install parse-prop-types

 
Parses React prop-types into a readable object at runtime.
$ npm install --save parse-prop-types
``jsx
import React from 'react'
import PropTypes from 'prop-types'
import parsePropTypes from 'parse-prop-types'
const MyComponent = ({ name, show }) => (
show ?
MyComponent.propTypes = {
name: PropTypes.string,
show: PropTypes.oneOfType([PropTypes.bool, propTypes.string]).isRequired,
}
MyComponent.defaultProps = {
name: 'Haz',
}
parsePropTypes(MyComponent)
`
The returned object is compatible with react-docgen:
`js`
{
name: {
type: {
name: 'string',
},
required: false,
defaultValue: {
value: 'Haz',
},
},
show: {
type: {
name: 'oneOfType',
value: [
{ name: 'bool' },
{ name: 'string' },
],
},
required: true,
},
}
?react-docgen reads file contents in order to find prop types definitions. It has some limitations, such as not allowing computed prop types and, in several situations, not being able to parse file contents correctly.
parse-prop-types, on the other hand, doesn't deal with file contents. Instead, it parses prop types at runtime by receiving the component object itself.
#### Table of Contents
Parameters
- $0 any $0.propTypes
- (optional, default {})$0.defaultProps
- (optional, default {}`)
Returns Object
MIT © Diego Haz