Skip to content
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 proposal: react/no-will-mount #1147

Closed
jacobrask opened this issue Apr 11, 2017 · 22 comments
Closed

Rule proposal: react/no-will-mount #1147

jacobrask opened this issue Apr 11, 2017 · 22 comments

Comments

@jacobrask
Copy link

There are few use cases for componentWillMount that cannot be handled in the constructor.

It is a common mistake to put asynchronous requests in componentWillMount which could potentially cause hard to find errors.

@ljharb
Copy link
Member

ljharb commented Apr 11, 2017

I disagree; i think everything that's not initializing state belongs in componentWillMount.

Async requests in the constructor have the same issues; neither us often a problem in practice.

I'm a strong -1.

@jacobrask
Copy link
Author

Wouldn't most async requests in practice set state on completion?

From my POV the question here is if there are a reasonable number of developers considering putting asynchronous requests in componentWillMount a problem, and that most other use cases can be handled in the constructor.

I think the React docs imply this.

Avoid introducing any side-effects or subscriptions in this method.
...
Generally, we recommend using the constructor() instead.

My understanding (which is very limited, so please correct me if I'm wrong) is that async rendering with fibers means that an async request might complete before mount and sometimes after (maybe the mount could even be aborted?).

(I would be willing to implement this if I can be reasonably sure that it would be accepted.)

@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

Side effects in JS never belong in constructors. If React advises that, they are wrong.

@jacobrask
Copy link
Author

My quotation was a bit confusing, those were excerpts from different paragraphs.

@ljharb
Copy link
Member

ljharb commented Apr 12, 2017

Indeed, fiber may change the timing of some of these things. But fiber isn't out yet, and if the issue is doing async things prior to the first render, then constructor vs componentWillMount has the same problem.

In other words, I'm still not seeing a reason to discourage componentWillMount - you've only talked about async setStates initiated prior to mounting.

@lukeapage
Copy link
Contributor

tracking: facebook/react#7671

@jacobrask
Copy link
Author

It seems like a significant number of React developers agree that using this method should be discouraged. If you do not agree it's of course fine to not use the rule.

Are you opposed to (me?) adding the rule at all to this project?

@ljharb
Copy link
Member

ljharb commented May 15, 2017

I'm not sure where you get "significant number" - that github issue is full of people who simply don't see a need for the method, with very very few claiming that its use should be discouraged.

So yes, I'm opposed to adding the rule at all.

@jacobrask
Copy link
Author

Ok

@ljharb
Copy link
Member

ljharb commented May 16, 2017

I'm not the only collaborator; this should remain open pending the others' thoughts.

@ljharb ljharb reopened this May 16, 2017
@abritinthebay
Copy link

Given it's an official FB recommendation (quoted above)

Generally, we recommend using the constructor() instead.

It would make sense to have this as an optional linting rule. Certainly shouldn't be a recommended one but a lot of the core devs seem pro-deprecation and have mentioned that post-fiber it would make sense to examine the issue more.

Most linting rules are completely subjective, this seems to at least be a reasonable suggestion for an optional rule.

@ljharb
Copy link
Member

ljharb commented May 25, 2017

Afaik there's still active discussions going on with core devs about it; I don't believe they've made a compelling case. Note that Facebook doesn't server-render, and their position on componentWillMount is one that is not shared by other stakeholders that do server-render.

@abritinthebay
Copy link

abritinthebay commented May 25, 2017

Sure, there are plenty of active discussions about many things - including things where rules already exist in this package.

That's what best practice debates are composed of 😄

My point is more - this kind of rule is a classic optional linting step feature. God knows there are existing rules in this package that I'm never going to turn on because they don't work for me or my projects. This rule would be no different to those.

Side note: I extensively use server-rendering and honestly, the story there is not as complex as some in that discussion would make it. Though yes, there are complexities involved and for those components that must use cWM then perhaps turning the rule off makes sense - but that's good, as it requires intentionality.

@ljharb
Copy link
Member

ljharb commented May 25, 2017

My comment stands, as does this one.

@abritinthebay
Copy link

Indeed, your preference is clear. If the rule existed you'd not use it/turn it off. 👍

@ljharb
Copy link
Member

ljharb commented May 25, 2017

To clarify; my preference is to not include the rule, so that nobody is ever able to turn it on.

@noahgrant
Copy link

this is a different context/discussion and a different rule, but now that React 16.3 is going to be deprecating cWM, cWRP and cWU, i'd love to have a linting rule that we could put in place to prevent using these methods as we migrate away from them. should we create a new discussion for it?

@ljharb
Copy link
Member

ljharb commented Mar 19, 2018

It’s not yet possible to recommend alternatives for every use case, nor have they actually been deprecated yet.

When React provides enough replacements for cWM etc, then we’ll definitely make linting rules to cover them.

@Hypnosphi
Copy link
Contributor

Here they are: https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html

@Hypnosphi
Copy link
Contributor

Generally, it would make sense to me to have a forbid-methods rule, where one could list the methods that shouldn't be used

@abritinthebay
Copy link

Oh that's a quite nice generalized rule, could even have a recommended/default set.

@ljharb
Copy link
Member

ljharb commented Apr 10, 2018

This seems handled by #1750.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants