-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imported propTypes triggers false positives #2357
Comments
Have the same issue. |
@monsieurnebo Hi! // MyComponent.jsx
import React from "react";
import PropTypes from "prop-types";
const propTypes = {
steps: PropTypes.array
};
function MyComponent(props) {
// Using steps.map somewhere
} but if I change the default import of propTypes to a single import from the "prop-types" module the error is gone. // MyComponent.jsx
import React from "react";
import { array } from "prop-types";
const propTypes = {
steps: array
};
function MyComponent(props) {
// Using steps.map somewhere
} Hope this will help you also. |
You should never have to destructure off of PropTypes to have things work; that’s a bug. The OP example with sharedPropTypes just won’t work with static analysis; I’d suggest not sharing the entire propTypes object and instead sharing only individual shapes or propTypes. |
That's really strange, because I have the same situation in another project (the same props, shared between components), without the lint error... There must be a difference, that could explain the error... But I'm struggling to find it. EDIT: Bingo 🎉 I found the difference:
ESLint error 🚨import React from "react";
import sharedPropTypes from "./sharedPropTypes";
const propTypes = {
...sharedPropTypes,
};
function MyComponent(props) {
// Using steps.map somewhere
} No ESLint error ✅import React from "react";
import sharedPropTypes from "./sharedPropTypes";
class MyComponent(props) extends React.Component {
static propTypes = {
...sharedPropTypes,
}
// Using steps.map somewhere
} However, I have no clue of the explanation behind this behavior. |
Where did you assign propTypes to MyComponent.propTypes in the SFC version? |
Sorry, I missed the props assignation in the pseudocode. It looks like this: import React from "react";
import sharedPropTypes from "./sharedPropTypes";
const propTypes = {
...sharedPropTypes,
};
function MyComponent(props) {
// Using steps.map somewhere
}
MyComponent.propTypes = propTypes; |
what happens if you do |
Same error :| |
In that case it really does seem like we're doing something different for classes and SFCs, so that's a bug. |
Good to know! |
@monsieurnebo can you confirm this is only happening inside of a nested function you’ve made that contains JSX and gets called inside of the return of the component? We had this happening in this scenario; but not if the prop is accessed directly in the return of the component. Eslint seems to think the nested function is a SFC component itself so it thinks it’s missing props in the parameter of the nested function. |
@jseminck I'm not sure to fully understand your scenario 🤔 |
Here's an example:
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. |
Thanks for the clarification, I have a better understanding of it now. import React from "react";
import sharedPropTypes from "./sharedPropTypes";
const propTypes = {
...sharedPropTypes,
};
function MyComponent(props) {
const renderFoo = () => {
<div>
{props.steps.map(item => ...)}
</div>
}
return (
<div>
{renderFoo()}
</div>
);
}
MyComponent.propTypes = propTypes; And the error goes away by passing the props as you said: import React from "react";
import sharedPropTypes from "./sharedPropTypes";
const propTypes = {
...sharedPropTypes,
};
function MyComponent(props) {
const renderFoo = (passedProps) => {
<div>
{passedProps.steps.map(item => ...)}
</div>
}
return (
<div>
{renderFoo(props.steps)}
</div>
);
}
MyComponent.propTypes = propTypes; Passing props to nested functions can be a workaround for now, but it would be nice that ESLint understands it by itself (if that's something doable 🤔 ). |
I was having the same issue on version |
@ljharb I think we can close this one. As you said in #2357 (comment) importing propTypes won't work with static analysis and using a variable, defined in the same file, was fixed by #2704 |
Hello,
Context
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:
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:
A lead
After some searches, it seems that ESLint is more sensitive since #2301 .
Related issues
#2352
#2343
The text was updated successfully, but these errors were encountered: