Add support for imported propTypes
See original GitHub issueImporting prop types is a fairly common use case which doesn’t appear to be possible at this stage. Any plans to support this pattern?
Example:
// defaultPropTypes.js
import { PropTypes } from 'react';
export default {
prop1: PropTypes.string
};
// otherPropTypes.js
import { PropTypes } from 'react';
export default {
prop2: PropTypes.object
};
// MyComponent.jsx
import { Component } from 'react';
import defaultPropTypes from './defaultPropTypes.js';
import otherPropTypes './otherPropTypes.js';
export default class MyComponent extends Component {
static propTypes = {
...defaultPropTypes,
...otherPropTypes
}
// etc...
}
Results in:
{
"MyComponent.jsx": {
"description": ""
}
}
Issue Analytics
- State:
- Created 8 years ago
- Reactions:107
- Comments:50 (6 by maintainers)
Top Results From Across the Web
How to Use PropTypes in React - freeCodeCamp
We can use PropTypes to validate any data we are receiving from props. But before using it we will have to import it...
Read more >How to validate React props using PropTypes - LogRocket Blog
Learn how to validate props with React PropTypes, React's internal mechanism for adding type checking to component props.
Read more >Need to import prop types in React? - Stack Overflow
Say I have a class and at the end I want to make it explicit in my code that it has no props...
Read more >Typechecking With PropTypes - React
import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( <h1> ...
Read more >prop-types - npm
import React from 'react'; import PropTypes from 'prop-types'; class MyComponent extends React.Component { render() { // ... do things with ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I started hacking on this today and managed to get importing working for propTypes, flow, and typescript types. See https://github.com/devongovett/react-docgen/compare/typescript...devongovett:importing. I’ll open up a PR once the typescript PR (#348) is merged, since this builds on that. In the meantime, feel free to try out my branch.
Current limitations:
import * as foo from '...'
)export * from '...'
)node_modules
is supported, but flow libdefs (flow-typed
folder,.js.flow
files, and.d.ts
files) are unsupported. Would need to reimplement a custom resolver for those.Please let me know if you try it out and encounter bugs.
PR opened: https://github.com/reactjs/react-docgen/pull/352