Rule proposal: disallow referencing another component's propTypes
See original GitHub issueSome 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:
- Created 7 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top 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 >
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
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.
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)