-
-
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 require-default-props
does not support default parameters in stateless function
#1009
Comments
Default values are not the same as defaultProps. The rule correctly reports this as an error. |
If so.. But I guess this kind of pattern is commonly used on stateless functions. So, this is reasonable to me. In order to realize this pattern works, may I request another rule or option? |
That's why it's an important rule - it's bad to use default values instead of defaultProps. I suppose we could make an option that allows default values but I think the existence of the option would be harmful. |
@ljharb Correct me if I am wrong, but this rule is unnecessary when we use flowtype. |
@steida i don't understand why it would be - |
Yeah, but if you have flowtyped everything, you need runtime checking only at application boundaries. |
@steida again, you're talking about |
I don't want to use defaulProps https://twitter.com/estejs/status/812491172438560769 |
As for optimizations, link pls? |
@steida There's no need for a link - by spec, default values are re-evaluated on every invocation of the function. If you don't want to use |
Hmm - yet the documentation for Flow / React specifically tell you to use default values this way and actually would not support propTypes. // @ https://flow.org/en/docs/frameworks/react/
let MyComponent = ({ foo, bar = 2 }: { foo: number, bar?: number }) => {
return <div>{foo + bar}</div>;
}; |
The documentation for React suggests a number of bad practices amidst all the good ones - just because they make the framework doesn't mean they're always right :-) |
#truth -- I switched to using defaultProps already and working well. Would be cool to have a plugin to automatically convert to defaultProps in this situations -- although I know there could be times default parameters are the desired effect (when it executes a function, etc). |
Hi guys, In my opinion there should be at least an option to tell that require-default-props should pass if there is default parameter is set.
Would you be so kind to add this option? |
That’s not included intentionally; react components (SFCs included) should simply not use default arguments at all. |
reactjs/rfcs#107 |
Not "will" - that's not merged yet. |
@ljharb can you please expand on your opposition to default arguments. Considering the React team seems pretty set on deprecating them for function components, and for good reason, what is your opposition to them? Or at the very least, what is your opposition to supporting it as an option? It seems you're out of step with where the React team is going. It seems to be that Our team is moving to use default arguments instead of |
I know this is an older thread, but I noticed it because it was referenced by a new RFC. Can you clarify what info and optimizations these comments were referring to? To my knowledge, React does not use |
@bvaughn at the time i made that comment, my understanding was that this was planned, but never materialized. |
@nathggns what is planned to be deprecated isn’t relevant until it actually is deprecated. However, in particular, defaultProps can be read and reflected on by HOCs and runtime tools attempting to inspect components, where default arguments can not - so i hope the RFC doesn’t land (even if it’s very likely to do so). |
I genuinely don't understand how you can believe this to be true. People are writing code now, and people can see what the React team is planning, and its pretty clear that It is also the case that if you know a feature is going to be deprecated in the future, that people making tooling that people depend on should be prepared for it. It makes no sense at all to wait literally until the release deprecating the feature before beginning to prepare options for avoiding the use of that feature. TL;DR: people know that defaultProps is going to be deprecated. That genie is out of the bottle. The question now is whether people making tooling are going to prepare for that now or later, and I don't understand why you would pick later. |
I assume React, like it usually does, will do the responsible thing and release a codemod to convert defaultProps to default arguments (assuming this change happens) - and at the same time, this plugin will add an autofixable rule to enforce defaultProps placement on function components. I'd pick later because nothing is certain until it's released - parts of hooks, for example, changed prior to release in ways that would have been complicated to transition alpha support to. |
I agree that nothing is certain until its released, but even if React decides not to deprecated defaultProps (which seems quite unlikely at this point), people are using default arguments now. People believe it is going to be deprecated and are therefore avoiding it. Is it your opinion that that is a mistake? |
@nathggns Sorry, but why do you see it as such a problem to simply disable this rule and do the default arguments right away? It's not mandatory to use rules that are most likely obsolete. Edit: Sure, you don't have the rule to help you writing default arguments, but is it such a pain? Personally, I rather rely on TypeScript than some linter telling me what to do. |
I think this seems a bit misleading. Default parameters are a language feature so React and the ecosystem don't really need to "support" them. Maybe you're referring to the fact that if both default parameters and |
No offense, but you might be slightly behind (perhaps even biased?) on what react ecosystem does. Have a look at any new library that uses function components. I tried a few I can recall and haven't seen a https://unpkg.com/[email protected]/cjs/react-router-dom.js |
@nathggns I wonder will you be able to work on PR for the new rule? Considering the linked PR (now merged), it's confirmed that React 17 will drop Also, shouldn't the current rule warn about deprecation? React will warn in runtime, but would be nice to have a warning in linter as well. @ljharb What are your thoughts here after your involvement in the facebook/react#16210? |
@FredyC I almost feel like there's two rules to come out of this one to prevent use of Should we make two new issues for this? I'm not sure if the rules have a naming convention or not. |
@nathggns Well, I am convinced that current |
This seems like an odd place to put such a deprecetation warning, would it not be better to have a |
People might have this rule enabled and it is forcing them to set |
@FredyC That's a fair point. Maybe this rule should be deprecated in favour of two new rules – one disabling defaultProps for function components and one requiring them only for class components? |
That's what I was mostly suggesting :)
There is probably no reason for deprecating that rule for class components and disturb users with that. Only new rule at this point should be about the proper use of default argument values and that's what we have been talking about for a while here ... #1009 (comment) |
The first step is that A different rule should exist that warns when defaultProps is used on SFCs; ideally doing nothing until the version of React that deprecates it (perhaps the next one, but not any of the current ones). |
That option should be enabled by default now because of With React 17+ the rule should emit a hard error when |
@FredyC it's not deprecated in a released version of React yet; it's just merged to master. |
And why does that matter? Obviously, the React team is already decided to deprecate it. By delaying it here you are only increasing the number of users who will have to refactor later because by default they will be forced to use Btw, don't forget that most users are probably using not ejected CRA apps. They will have no way to enable that option or disable the rule. |
I'm not delaying anything; I'm not going to add a breaking change for any reason - and for those using tools that rely on defaultProps - like storybook annotations, etc - silently no longer warning on these could break them. This may not be your use case, but that doesn't mean I'm going to break it. |
What are you talking about? I am not suggesting to be forbidding to use |
It seems like the happy medium is the following:
While some of us including @FredyC & I may like to go further than this, if @ljharb can agree to this bit of work being done now (by pull request or by a core maintainer) it might be worth starting work on what we can agree on rather than letting "perfect" (from our perspective) be the enemy of good. @ljharb can you confirm that this is a path you'd be supportive of? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I’m going to continue using the term SFC as long as the wider community does; the react team’s naming preference doesn’t constrain anyone else. @FredyC the issue is that for some use cases, merely omitting defaultProps on an SFC will break things, because tooling requires it be present. That tooling certainly will have to adapt to lacking this information as React releases the deprecation, but until that time, omitting the defaultProps on a single component will break people. @nathggns that plan is exactly what i was suggesting; except that nothing can be added to recommended without a breaking change, so we won’t be doing that. I’d be comfortable enabling the “don’t warn on missing default props on SFCs” option in the recommended config, however, since that warns less, and anyone depending on airbnb or on every component having defaultProps will not be affected by that. Let’s do each thing in a separate PR, and naming bikesheds can happen there. |
Oh man, I don't know. It seems like I am either explaining it badly or you are not reading what am I saying. Let's be practical. If someone uses tooling that relies on
So you are going to name rules with I agree we should discuss further approaches in separate issues/PRs |
That user is making that choice by using the current rule in master. Silently taking away their choice by defaulting them to a permissive mode is unacceptable. The hooks have state, but the hooks aren’t the component - imo they’re still forever stateless. |
There is a problem in that logic. What if you have class components which still needs Ultimately, this whole rule should be deprecated and there should be separate ones for class and FC, but that's surely a question of some semi-distant future.
Uh, what? :) Hooks are not components, that's true, but hooks are used in components thus making them stateful and no longer stateless. Have you ever used hooks seriously? Cannot imagine what would you say something like that 😆 |
This comment has been minimized.
This comment has been minimized.
@FredyC it's not misleading if you are using tools that break when an SFC lacks defaultProps, as some people are doing now. |
As for SFCs, they were SFCs - stateless - even if they closed over variables, giving them state - the components are not stateful, they just might close over state. In the case of hooks, the hook is doing the same thing. |
Hello, I just got some error from
require-default-props
rule:Here is the code:
I expected there is no error, but seems it causes the problem.
Any ideas?
The text was updated successfully, but these errors were encountered: