Skip to content

Rule proposal: disallow referencing another component's propTypes #696

@lencioni

Description

@lencioni

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions