eslint-plugin-react: react/prop-types - is missing in props validation - false/positive when using inner function
Hi, I came across the issue with react/prop-types rule.
import React from 'react';
import PropTypes from "prop-types";
const DisplayName = (props) => {
const getNameDiv = () => {
return <div>{props.name}</div>;
};
return getNameDiv();
};
DisplayName.propTypes = {
name: PropTypes.string.isRequired,
};
gives me warning 'name' is missing in props validation react/prop-types. Same thing happens if I rewrite function to function getNameDiv() {...}
After inlining getNameDiv there is no warning:
import React from 'react';
import PropTypes from "prop-types";
const DisplayName = (props) => {
return <div>{props.name}</div>;
};
DisplayName.propTypes = {
name: PropTypes.string.isRequired,
};
I was looking for similar issues but I didn’t find any. Maybe https://github.com/yannickcr/eslint-plugin-react/pull/1605 is connected?
I was using eslint-plugin-react: 7.11.1 but it’s the same for 7.13.3.
Thanks!
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 22
- Comments: 28 (11 by maintainers)
Commits related to this issue
- Downgrade eslint react/prop-types rule to warning. From https://github.com/yannickcr/eslint-plugin-react/issues/2135, it seems that this may be an error in the eslint plugin? — committed to blink1073/jupyterlab by jasongrout 4 years ago
- Downgrade eslint react/prop-types rule to warning. From https://github.com/yannickcr/eslint-plugin-react/issues/2135, it seems that this may be an error in the eslint plugin? — committed to blink1073/jupyterlab by jasongrout 4 years ago
- [Tests] `prop-types`: add passing test Closes #2135 — committed to ljharb/eslint-plugin-react by ljharb 2 years ago
Originally from https://stackoverflow.com/a/60510524/985454
I have discovered a strange behaviour. The only difference is in the return statement:

Now also the

barinpropTypesis uncommented:I think the problem is because the

myFunclooks like a functional component and that eslint is picking it up wrongly.Also, this is rather funny, if you simply rename the

propsto anything else, eslint goes silent 😃Seems to be trigggered as soon as there’s not JSX returned? It just popped out of the blue for me…
Okay one could:
But still…
In this case,
getNameDivis actually being detected as a component (which it should be; prefer components to render functions).I’m not sure how we could rule out
getNameDivas being a component without ruling out many valid use cases.Not always. Making a component involves quite a lot of boilerplate; sometimes within a component it’s easier to use a simple inline function. I find it’s much better from a readability perspective to replace trees of ternary statements with a function containing a series of if/then statements.
This is pretty gross, especially with more conditions:
This is more readable:
One of the nice things about working with jsx/tsx is that “it’s just javascript”. This lint rule rejects a common javascript idiom.
So I had 30 minutes to correct a bug at line 209 of material-ui-treeview. It’s a simple, one-file module with a permissive license, so I copied it into my code base and made the (trivial) change, then spent the next 19 minutes puzzling over how to remove this lint error before I disabled the rule for the module, then 5 minutes on the comment above. I’m referencing the specific file because I think it may provide a good concrete example of what I meant by “burdensome”. The location responsible for the errors (line 152) is a recursive function within a component; the function itself behaving as a component but needing its closure, and I couldn’t think of a quick way to refactor it to satisfy this rule. Further, it seemed to me that the design was suited to purpose, and any refactoring to eliminate the rule error would degrade the module’s design. This may be an exception case, and perhaps disabling the rule is the expected remedy, and I BTW agree that OP’s example is not ideal and think that detection of the anti-pattern is a good thing, but I also believe there’s a critical distinction between interface and implementation, and that the prop-types rule should be about interface, not implementation, because PropTypes is about interface, not implementation.
Certainly that’s subjective; personally I’d find it much more readable for
noNamesto be inline jsx, and fornamesTableto be a separate component.Let’s leave this open in case there’s a way to handle it.
Hello, This post can resolve your problem.
Look this: http://www.hackingwithreact.com/read/1/41/how-to-add-react-component-prop-validation-in-minutes
but he is using it in a deprecated way, to solve this.
Make this like:
YourFunction.propTypes = { params: PropTypes.object.isRequired, };