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.

Imported propTypes triggers false positives

See original GitHub issue

Hello,

Context

Package Version
eslint ~6.0.1
eslint-plugin-react ~7.14.2

The situation

After updating ESLint, I’m encountering false positives when importing propTypes (that are used in multiple places, for instance).

Let’s take a simple example:

// sharedPropTypes.js

import PropTypes from "prop-types";

export default {
  steps : PropTypes.array,
};
// MyComponent.jsx

import React from "react";
import sharedPropTypes from "./sharedPropTypes";

const propTypes = {
  ...sharedPropTypes,
};

function MyComponent(props) {
  // Using steps.map somewhere
}

This scenario is triggering the following lint errors: error 'steps.map' is missing in props validation react/prop-types

Which is new.

Please note that declaring propTypes in the component file is working fine:

// MyComponent.jsx

import React from "react";
import PropTypes from "prop-types";
import sharedPropTypes from "./sharedPropTypes";

const propTypes = {
  steps : PropTypes.array
};

function MyComponent(props) {
  // Using steps.map somewhere
}

A lead

After some searches, it seems that ESLint is more sensitive since https://github.com/yannickcr/eslint-plugin-react/pull/2301 .

  1. Could it be the source?
  2. How should I manage this situation in a clean way?

Related issues

https://github.com/yannickcr/eslint-plugin-react/issues/2352 https://github.com/yannickcr/eslint-plugin-react/issues/2343

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jstheoriginalcommented, Aug 2, 2019

@jseminck I’m not sure to fully understand your scenario 🤔 Could you provide a simple example of what you’re talking about?

Here’s an example:

interface Props {
  data: {
    text: string,
    icon: Image,
  },
}

const Button = ({ data }: Props) => {
  ...

  const renderLabel = () => <Text>{data.text}</Text>;

  return (
    <View>
      {renderLabel()}
    </View>
  );
};

This ESLint rule should complain that data.text is missing in props validation. The reason seems to be because ESLint thinks that renderLabel const is a different component of its own and needs to have props of its own. The error goes away if you pass the props in to the parameters of renderLabel.

1reaction
jzabalacommented, Jul 12, 2020

@ljharb I think we can close this one. As you said in https://github.com/yannickcr/eslint-plugin-react/issues/2357#issuecomment-514675480 importing propTypes won’t work with static analysis and using a variable, defined in the same file, was fixed by #2704

Read more comments on GitHub >

github_iconTop Results From Across the Web

Imported propTypes triggers false positives #2357 - GitHub
After updating ESLint, I'm encountering false positives when importing propTypes (that are used in multiple places, for instance). Let's take a ...
Read more >
false positive in prop-types validation - reactjs - Stack Overflow
You are not doing anything wrong. The problem is that every function that returns a JSX component is technically a stateless component.
Read more >
Don't Call PropTypes Warning - React
This pattern by itself is fine, but triggers a false positive because React thinks you are calling PropTypes directly. The next section explains...
Read more >
prop-types - UNPKG
1, # prop-types. 2. 3, Runtime type checking for React props and similar objects. 4. 5, You can use prop-types to document the...
Read more >
'name' is missing in props validation - You.com - You.com
This message appears because you are using PropTypes to validate your props types. Basically, it's a validation of the type of data you...
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