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.

Rule proposal: disallow referencing another component's propTypes

See original GitHub issue

Some people might want to use babel-plugin-transform-react-remove-prop-types to remove propTypes from their components in production builds, as an optimization. This transform changes code like:

const Baz = () => (
  <div />
);

Baz.propTypes = {
  foo: React.PropTypes.string
};

into:

const Baz = () => (
  <div />
);

This works, but might end up breaking things in production if you import a component and then reference that component’s propTypes. Instead, the propTypes should be separately exported in this case and referenced directly from the export.

Bad:

import { pick } from 'lodash';

import SomeComponent from './SomeComponent';

export default function AnotherComponent(props) {
  const passedProps = pick(props, Object.keys(SomeComponent.propTypes));
  return (
    <SomeComponent {...passedProps} />
  );
};

Good:

import { pick } from 'lodash';

import { propTypes: someComponentPropTypes }, SomeComponent from './SomeComponent';

export default function AnotherComponent(props) {
  const passedProps = pick(props, Object.keys(someComponentPropTypes));
  return (
    <SomeComponent {...passedProps} />
  );
};

This rule would make the aforementioned Babel plugin safe to use.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Jan 20, 2017

The goal is to be able to strip propTypes but ensure that doing so won’t break any code.

I don’t have any intuition about where the best place for the error is, but having it in eslint might let it be caught earlier.

0reactions
hzoocommented, Jan 20, 2017

If this is specific to babel-plugin-transform-react-remove-prop-types, we could just error from the plugin itself instead of the eslint rule (just depends on whether we think its better to make it independent of not)

Read more comments on GitHub >

github_iconTop Results From Across the Web

disallow referencing another component's propTypes · Issue ...
Rule proposal : disallow referencing another component's propTypes #696 ... This rule would make the aforementioned Babel plugin safe to use.
Read more >
Switching between two different prop types in one React ...
I'm having some trouble with a React component props type definition. I have a component published on npm that currently takes a simple...
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 >
Props · Styleguide JavaScript
Avoid using DOM component props names for different purposes. props are expected to be like style and className to mean one specific thing....
Read more >
The React Handbook – Learn React for Beginners
The React Handbook follows the 80/20 rule: learn in 20% of the time ... Styled Components offer other functions you can use to...
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