-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Description
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.
oliviertassinari