forbid-foreign-prop-types: Allow setting propTypes
See original GitHub issueTo use PropTypes.instanceOf
and avoid circular dependencies I need to set propTypes outside of the “target” component. However forbid-foreign-prop-types
kicks in.
const PropTypes = require(`prop-types`);
const React = require(`react`);
const B = require(`./B`);
class A extends React.Component {
render(){
return <B me={this} />;
}
}
B.propTypes.prop = PropTypes.instanceOf(A).isRequired;
module.exports = A;
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
eslint-plugin-react/forbid-foreign-prop-types.md at master
This rule forbids using another component's prop types unless they are explicitly imported/exported. This allows people who want to use babel-plugin-transform- ...
Read more >How to disable ESLint react/prop-types rule in a file?
I had to do this in my .eslintrc config file to disable prop types validation error.
Read more >Common code mistakes in React that you (maybe) made
(react/forbid-foreign-prop-types) And let's continue with propTypes. Don't use propTypes from another component like this:
Read more >Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >How To Customize React Components with Props
They also give you the chance to define default data in cases where the prop is not always required. Unlike most type systems,...
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
Actually I don’t think passing a component instance to a child is a good practice. I’m even thinking of a rule that throws when such behaviour is present.
Switching to PropTypes.shape and closing.
If B is requireable, then it’s just as exposed as A 😃 However, if you’re going to have circular dependencies - something you should avoid strenuously - you can’t use the otherwise best practice of
module.exports =
. Doexports.A = A;
andexports.B = B
, and then your circular dependencies will work fine.