question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

forbid-foreign-prop-types: Allow setting propTypes

See original GitHub issue

To 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;

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
DimitarNestorovcommented, Jun 8, 2018

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.

1reaction
ljharbcommented, Jun 8, 2018

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 =. Do exports.A = A; and exports.B = B, and then your circular dependencies will work fine.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found