Skip to content

rerodrigues/react-proptypes-from-json

Repository files navigation

React PropTypes from JSON

Auto generate React PropTypes from a JS Object

Instalation

npm install @renatorodrigues/proptypes-from-json

Reference

propTypesFromJson(json = {} <Object>)

Returns: propTypes <Object>

Usage

import propTypesFromJson from '@renatorodrigues/proptypes-from-json';

import MyComponent from './my-component';

const myProps = {
  "name": "The Rodrigues Family",
  "parents": [
    {
      "name": "Sergio",
      "age": 61
    },
    {
      "name": "Gislaine",
      "age": 59
    }
  ],
  "children": [
    {
      "name": "Renato",
      "age": 37
    },
    {
      "name": "Erika",
      "age": 34
    }
  ]
};

<MyComponent {...myProps} />

MyComponent.PropTypes = propTypesFromJson(myProps);

The props will be converted to:

{
  name: PropTypes.string,
  parents: PropTypes.arrayOf(
    PropTypes.shape({
      name: PropTypes.string, age: PropTypes.number
    })
  ),
  children: PropTypes.arrayOf(
    PropTypes.shape({
      name: PropTypes.string, age: PropTypes.number
    })
  )
}

2019 - Renato Rodrigues

About

Auto generate React PropTypes from a JS Object

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published