Auto generate React PropTypes from a JS Object
npm install @renatorodrigues/proptypes-from-json
propTypesFromJson(json = {}
<Object>)
Returns: propTypes
<Object>
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