-
-
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
Allow prop-types rule to skip non-exported SFCs #494
Comments
array method callbacks may make sense to skip, but standard SFCs should definitely always have propTypes. |
I'm having a weird issue at the moment with prop-types.
I end up with warnings for videoJsControlBus missing in props validation. I think it's detecting this particular case as a stateless function component, which technically it's not? Thus ignoring stateless components would be a highly desirable option. |
@darrennolan in this case, The goal is to catch bugs as soon as possible - even though VideoJSPlayer will catch it, your component should be catching it first. |
Thanks for the super quick reply @ljharb =D But getComponentStream doesn't actually return a new react component. I should update that code a bit better. To me, that would be like having to declare prop types against the following, when Class PlayerComponent already has them defined.
Unless in these cases it's better for me to explicitly return |
Could you provide a more accurate example? If your function doesn't directly return JSX, it shouldn't be identified as needing propTypes. |
Trimmed down version of the function, but this shows the streams we muck about with and then return a stream that when subscribed - gives us this react component with the props set accordingly.
The props needed warning appears on the function getVideoJSComponentStream arguments. Which may or may not be expected. |
I'm not sure what the core objection is here — we use PropTypes extensively across our application, at multiple boundaries and depths. We only use Stateless Function Components within class components with propTypes defined (and enforced, thanks to this plugin) so it's both unnecessary to re-declare the same propTypes again for SFCs and affects readability of our code. I understand not everyone works in this way, which is why it would only be optional at best. I don't have experience with ASTs and eslint but if it's too hard to deal with here I'll try to find some time soon to get a PR together. Thanks. |
@darrennolan that looks like a bug in that it's mistaking the JSX in the .map for your function being an SFC. i'd file a separate issue about that. |
+1, turning this off for stateless function components is exactly what I need. They get used for one-offs, which don't make sense to turn on prop type checking for. |
I would be fine with a rule stating that un-exported SFCs don't need propTypes. However, any exported SFCs should have the same propType checking as any other kind of exported component, and I would be a strong -1 on any propTypes rule that differentiated between components solely based on their statefulness/statelessness. |
That happens to work for my use case, so fine by me. Although I have to say, I don't really understand the extremely rigid take on it. The whole point is that it's an option you wouldn't have to ever use if you disagree so strongly with it. But for other folks it makes sense that when they write one-or-few-liner components they don't want to have to be forced to add prop types. Either way, I've just abandoned this rule now anyways. |
This would be great. Is there any progress towards this yet? |
It's not a false negative, it's a correct error - but yes indeed, this proposal would stop warning there. |
Really? Even though that function is called directly and not with |
@amannn ah sorry, i misread. yeah, in that case it's our SFC detection, since the function returns jsx and isn't a callback, it looks like an SFC. |
if i have function foo(props){
return <div>{props.whatever2}</div>;
}
function ActualComponent(props){
return (
<div>
<div>{props.whatever}</div>
{foo(props)}
</div>;
}
ActualComponent.propTypes = {
whatever: PropTypes.string.isRequired
};
export default ActualComponent. the linter should recognize that foo is not a component and doesn't need proptypes, and that ActualComponent is actually using the whatever prop, and that acutal component is missing a proptype for whatever 2, right? is it possible to trace through the function calls to see which props are getting used by helper functions? and to not expect proptypes on functions that are called explicitly by other parts of the file? |
The recommendation there is that That said, since |
that makes sense. |
From the docs:
It would be great if we could enforce propTypes being defined on
React.createClass()
andReact.Component
-extended components, but not stateless functions which return JSX.In fact, defining
.propTypes
in function use such as{ arr.map((name, i) => <Row key={ i }>{ name }</Row>) }
would increase verbosity considerablyThe text was updated successfully, but these errors were encountered: