-
-
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
[BUG] react/prop-types - false positive in functional components #2607
Comments
Why would you use a function like that? |
@ljharb Is this information important for the given bug? There are many reasons why you should structure your code into small functions. Using the Single Responsibility Principle or Top-Down design is state of the art nowadays.
(Bug occurs in this code also) |
It's a bad idea to pass the props object around anywhere, so even if you wanted to do that, you'd always destructure props directly in the component, and pass them to the function you wanted. If you do that, the linter will work correctly as well. There's nothing about that code that's "state of the art", imo - |
What if I want to break my component into smaller components within same file and then call them as needed. For Example:
Now let's just skip the code quality thing for a moment but the above example gives me false error as well. |
The |
With the improvement in function component detection proposed in PR #2699, this error is gone since the const MyComponent = (props) => {
function render() {
return <test>{props.hello}</test>;
}
console.log(props.hello) // no error here since MyComponent is not recognized as a component
return render();
};
MyComponent.propTypes = {
text: PropTypes.string.isRequired,
}; |
That's great to hear. I'm not too worried about this edge case - that pattern seems nonsensical to me. |
When I use props inside a function of a functional component. The eslint PropTypes validation fails. Which is wrong.
My eslintrc.json:
Example with bug
Error messge:
6:29 error 'text' is missing in props validation react/prop-types
If I don't use a function inside my functional component. The eslint rule validates my code correctly.
Working example:
The text was updated successfully, but these errors were encountered: