-
-
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
defaultProps
rule to be deprecated on function components
#2396
Comments
That won't change by default; there can be an option to ignore function components, however. |
Is there a way to check the deconstructed values for default props instead? |
@KevinGhadyani-minted I'm not sure what you're asking. |
The change isn't to remove Where we once had: const TestComponent.defaultProps = {
size: 'medium',
type: 'primary',
} We now have: const TestComponent = ({
size = 'medium',
type = 'primary',
}) => (
// ...
) The goal isn't to remove I would expect the |
That's something that should be configurable via an option, as I said above. |
I think I'm confused then. In your comment, you said there can be an option to ignore it for function components. To me, this means there's development that would occur to disable it for function components, but this development has not occurred. It doesn't say there'd be an alternative for getting it to identify ES2015 default assignment instead. |
Yes, it means someone would have to do the development work to add an option. It makes sense to me to modify |
@ljharb, I'm considering working on the addition you suggested above, is there a good place to start beyond looking at the current rule implementation? |
@aaronmccall That, and its tests, are pretty much it :-) |
if you are using a state ,then use the default values like below,
|
Can we also have a way to enforce that function components can't have |
@chbdetta that's included in #2396 (comment) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The second variant is no less correct, it just doesn’t give some of the types names. Regarding our comments being hidden: Is this not the right place for this discussion? Whatever is the case, I very much think that my rules:
react/require-default-props:
- error
- forbidDefaultForRequired
overrides:
- files: ['*.ts', '*.tsx']
rules:
react/require-default-props: off # use default arguments in typescript |
@flying-sheep discussion about just disabling the rule, even with overrides, won’t likely help anyone; people already know they can do that. Either way, defaultProps should absolutely be used in TS still, and the type info does not contain the same information - because 100% of it is stripped out at runtime, which is when the defaultProps information can be needed. |
Would that very depending on the compilation target? I would expect default values assigned in ES6 to still work at runtime (be that built by TS or not), right? |
@amypellegrini they work, but are not introspectable, so at runtime tooling can’t read information about what’s defaulted except with .defaultProps. |
Why treat components in a special way? If you use JS, your code base could benefit from runtime type checking for all kinds of functions, not just components. Once you have fully migrated a code base to TypeScript, all types are proven correct at compile time and runtime checking isn’t necessary. |
This is objectively false, since every system includes runtime user data (from the network, filesystem, user interaction, etc), as well as a majority of third-party code that was never typechecked, and this false belief is actually the worst damage caused by the use of a type system. |
Even if TS was a sound type system - which it is not - it would NEVER be safe to omit runtime checks or to believe that types are "proven" correct. TS, for example, thinks that this JS snippet which has thrown an exception since 2009 - |
Well, seems like none of my systems ever put user data through edge cases like the one you pointed out, but point taken! |
#2396 (comment) is the plan of record here. |
If this gets depreacted, is there still a way to set the defaultProps globally for example the react-native |
@uloco im not familiar with what you’re describing, but either way, that’s a question to ask react native, and only if concrete plans to depreciate it ever materialize. |
Any way to disable the error that react throws in development about this? |
Does anyone have an idea whether this improvement is available now? Thanks in Advance! |
@pavinduLakshan at the top, it says "fixed by #3249", and if you go there, you can see my comment #3249 (comment) which says that it's in v7.30.0. |
Thanks. missed it. |
defaultProps
on function components is getting deprecated by React (see: facebook/react#16210)For this rule to follow suit, it should only be triggered when missing from class components.
The official recommendation in future versions of React is to use ES6 default values for function components.
The text was updated successfully, but these errors were encountered: