https://github.com/diegohaz/parse-prop-types
Parses React prop-types into a readable object
https://github.com/diegohaz/parse-prop-types
Last synced: about 1 year ago
JSON representation
Parses React prop-types into a readable object
- Host: GitHub
- URL: https://github.com/diegohaz/parse-prop-types
- Owner: diegohaz
- License: mit
- Created: 2018-02-04T19:14:07.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T19:46:21.000Z (over 3 years ago)
- Last Synced: 2024-10-19T10:04:57.809Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 995 KB
- Stars: 69
- Watchers: 2
- Forks: 5
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-projects - parse-prop-types - Parses React prop-types into a readable object ⭐69 `JavaScript` (📦 Legacy & Inactive Projects)
README
# parse-prop-types
[](https://github.com/diegohaz/nod)
[](https://npmjs.org/package/parse-prop-types)
[](https://travis-ci.org/diegohaz/parse-prop-types) [](https://codecov.io/gh/diegohaz/parse-prop-types/branch/master)
Parses React `prop-types` into a readable object at runtime.
## Install
$ npm install --save parse-prop-types
## Usage
```jsx
import React from 'react'
import PropTypes from 'prop-types'
import parsePropTypes from 'parse-prop-types'
const MyComponent = ({ name, show }) => (
show ?
{name} : null
)
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`](https://github.com/reactjs/react-docgen):
```js
{
name: {
type: {
name: 'string',
},
required: false,
defaultValue: {
value: 'Haz',
},
},
show: {
type: {
name: 'oneOfType',
value: [
{ name: 'bool' },
{ name: 'string' },
],
},
required: true,
},
}
```
**IMPORTANT:** To avoid issues reading any component's propTypes, it is recommendable importing this package before defining your components' propTypes or before import any third-party component. Import this package in your entry point file could be a great option.
```
// index.js (entry point file)
import "parse-prop-types";
```
## Why not [`react-docgen`](https://github.com/reactjs/react-docgen)?
[`react-docgen`](https://github.com/reactjs/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.
## API
#### Table of Contents
- [parsePropTypes](#parseproptypes)
### parsePropTypes
**Parameters**
- `$0` **any**
- `$0.propTypes` (optional, default `{}`)
- `$0.defaultProps` (optional, default `{}`)
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
## License
MIT © [Diego Haz](https://github.com/diegohaz)