babel-plugin-flow-react-proptypes icon indicating copy to clipboard operation
babel-plugin-flow-react-proptypes copied to clipboard

Identifier 'PropTypes' has already been declared

Open dht opened this issue 6 years ago • 2 comments

We use contextTypes so we have to import PropTypes:

ScreenHomepage.contextTypes = {
    locale: PropTypes.string,
    colors: PropTypes.object,
};

However, when using the plugin the build fails with:

./layout_modules/screen-homepage/ScreenHomepage.js 422:7 Module parse failed: Identifier 'PropTypes' has already been declared (422:7) You may need an appropriate loader to handle this file type. | }; | export default ScreenHomepage;

import PropTypes from "prop-types";

This is probably due to the fact the plugin auto adds the PropTypes import.

Is there an option to tell the plugin not to import PropTypes if the import exists?

dht avatar Dec 29 '18 00:12 dht

Not sure if you're still experiencing this, but it seems that the README says that it compiles the import as:

import bpfrp_PropTypes from 'prop-types';

But that doesn't appear to be the case.

As a workaround I've changed the manual imports to:

import ReactPropTypes from 'prop-types';

which seems to be working.

dcwither avatar Jul 11 '19 21:07 dcwither

Well, you can try to check if you are not using the same name as in the import and then re declaring it as a const with the same name.

For example, I have imported the PropTypes:

import PropTypes from 'prop-types';

And then I was trying to declare it again as a const:

const PropTypes = {
  navigation: PropTypes.shape({
    getParam: PropTypes.func,
  }).isRequired,
};

I just solved it by changing the const PropTypes to const propTypes with a lower case in the first char; Simples as that.

The result is:

import PropTypes from 'prop-types';

(...)

const propTypes = {
  navigation: PropTypes.shape({
    getParam: PropTypes.func,
  }).isRequired,
};

But if does not solves it, you can change the name of you import, like @dcwither already told it.

frederikocmr avatar Sep 14 '19 03:09 frederikocmr