-
-
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
Rule request: require-default-props #528
Comments
It would be great if this rule could take into account references to objects in other modules. e.g. // Component-shape.js
export default {
foo: React.PropTypes.string,
bar: React.PropTypes.string.isRequired,
} // Component.js
import ComponentShape from './Component-shape.js';
const propTypes = ComponentShape;
export default function Component({ foo, bar }) {
return <div>{foo}{bar}</div>;
}
Component.propTypes = propTypes; should warn about |
I would like to work on this if no one is on it already! |
It would be great if that rule allowed an option for non-required booleans to be excluded. Because if they are not provided, it will be a falsy value. |
Falsy, but not false. Relying on falsiness can result in implicit coercions that can cause hard-to-find bugs. If an option for that were added, it'd need to be off by default, but I'm -1 on adding that option. |
I agree that it should be off by default but I still this as a nice option. If it isn't provided, it's |
The implicit coercion would be in the component code itself, where it expects a boolean but sometimes gets |
@vitorbal are you actually working on this? would be a killer feature! |
Sorry, was a bit bogged own with other tasks in my pipeline ⏳ I'm still interested in pursuing this though, will try to make some time for it these next couple of weeks. |
Quick update: I've been slowly chipping away at this and covering all the different cases it would have to support. I will probably have a PR up as an RFC by next week. |
Okay, I finally managed to wrap up most of this rule, I think. The only thing missing is support for Flowtype. @ljharb @yannickcr How important would you say is flowtype support? Could I tackle that as a separate PR, for example? |
I think it's critical that it not break on code that has flow syntax, at the very least, but I'm not sure how critical it is that the initial PR support flow. It'd probably be ideal to include it at the outset, though. |
@ljharb fair enough, thanks for the feedback. In that case, I'll start working on adding Flow support as well. |
@yannickcr How to configure this rule in eslintrc ? There is no rule options given in the documentations. Also how to use this rule with |
@agupta-q4 the rule does not have any extra configuration besides just being turned "on" or "off". Is there any specific configuration that you are looking for? |
@vitorbal Thanks for the heads up ! :) yes you are right, I didn't understood it at first hand. actually whenever I try to add object type in propTypes definition, this rule says forbidden. so in react I added something like below to overcome this warning:
now it is working fine. also do you have any idea what is rule options for following two newly added rules:
(I am newbie in react - redux environment) |
This rule should accept a list of props that are excluded from the check. Similarly, we have a custom of merging given |
No, it's not perfectly fine - children needs a defaultProp too. It serves as documentation, and ensures that the author didn't make a mistake. Separately, having a className prop is not a great idea at all anyways: https://brigade.engineering/don-t-pass-css-classes-between-components-e9f7ab192785 but if you have it, you need the defaultProp there too. You're free not to use the rule, of course, if you don't like the rigor it provides. |
I don't agree on misusing Back on topic - if the rule accepted excludes, we would both be happy. I would exclude |
Anyway, here's a fork with support for "react/require-default-props": [
"error",
{
"ignore": ["children"]
}
] If anyone is interested, just put |
If you'd like to open a separate issue requesting that feature, we can discuss it. The benefit of having even an explicit (The |
I'd like a rule that requires an explicit
defaultProp
to be present for every non-required prop (inpropTypes
or otherwise).The text was updated successfully, but these errors were encountered: