Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

parse-prop-types

diegohaz346.9kMIT0.3.0

Parses React prop-types into a readable object

parse-prop-types, react, prop-types

readme

parse-prop-types

Generated with nod NPM version Build Status Coverage Status

Parses React prop-types into a readable object at runtime.

Install

$ npm install --save parse-prop-types

Usage

import React from 'react'
import PropTypes from 'prop-types'
import parsePropTypes from 'parse-prop-types'

const MyComponent = ({ name, show }) => (
  show ? <div>{name}</div> : 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:

{
  name: {
    type: {
      name: 'string',
    },
    required: false,
    defaultValue: {
      value: 'Haz',
    },
  },
  show: {
    type: {
      name: 'oneOfType',
      value: [
        { name: 'bool' },
        { name: 'string' },
      ],
    },
    required: true,
  },
}

Why not react-docgen?

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

Parameters

  • $0 any
    • $0.propTypes (optional, default {})
    • $0.defaultProps (optional, default {})

Returns Object

License

MIT © Diego Haz

changelog

0.3.0 (2018-07-10)

Bug Fixes

  • Ensure development prop-types (#5) (6a49527)

BREAKING CHANGES

  • prop-types will use the development version even in production.