eslint-plugin-react icon indicating copy to clipboard operation
eslint-plugin-react copied to clipboard

[New Rule] prefer-function-component

Open tatethurston opened this issue 4 years ago • 11 comments

Adds a new rule to enforce the use of function components over class components. Related issue: https://github.com/yannickcr/eslint-plugin-react/issues/2860

Users wanting to try this lint rule today:

This functionality exists in eslint-plugin-react-prefer-function-component.

npm install eslint-plugin-react-prefer-function-component

tatethurston avatar Aug 11 '21 23:08 tatethurston

#2860 doesn't have a "help wanted" or "accepted" label on it - I continue to be unconvinced this is actually a good rule to have. Some things must be class components, and other things are more clear/better as class components; I don't think a lint rule is naunced enough to be appropriate.

That said, I did ask you to open the PR, so I'll give it a good faith review.

ljharb avatar Aug 12 '21 06:08 ljharb

I wonder if perhaps this could be a more generic "prefer-component-style" rule, that can be used to force class components or force function components?

Yeah that seems reasonable to me, the motivating intent here is to keep consistency in a code base.

Looking at the recent work by the Relay team (and their close collaboration with the React team) and the React concurrent mode work my impression is that the longer term direction of React is going to be towards all function components, but that is speculative right now.

tatethurston avatar Aug 12 '21 15:08 tatethurston

#2860 doesn't have a "help wanted" or "accepted" label on it - I continue to be unconvinced this is actually a good rule to have. Some things must be class components, and other things are more clear/better as class components; I don't think a lint rule is naunced enough to be appropriate.

That said, I did ask you to open the PR, so I'll give it a good faith review.

I don't have any expectations here, just something to consider. This can always sit or be closed and reopened in the future. I just want things to be code complete (or close) while I have the time.

tatethurston avatar Aug 12 '21 15:08 tatethurston

Just wanted to add a voice of support for this approach. prefer-component-style does seem like a reasonable modification to make in the name of flexibility. That said, my/our intended use case is to steer toward function components, and I suspect this will be the case for the majority of codebases, as old class component lifecycle methods become deprecated, and the React community further embraces hooks as a standard. Either way, this seems like a valuable addition to eslint-plugin-react

rusackas avatar Oct 25 '21 23:10 rusackas

Just wanted to pop in to say I'd like to have this lint rule too. As pointed out elsewhere, the biggest value here is in enabling consistency across a codebase and the ability to reuse logic between components easily.

In my case, another big use is to use these lint rules to gauge how well we are/are not doing in paying down tech debt over time on an old-ish codebase where we're about 50/50 on function components versus class components. Given the large-ish size of our org and codebase, this has to be automated via tooling so we can create dashboards/reports/etc.

nebrius avatar Jun 23 '22 23:06 nebrius

Does the function-component-definition rule meet everyone's use cases here?

ljharb avatar Aug 05 '22 04:08 ljharb

Does the function-component-definition rule meet everyone's use cases here?

No that rule enforces the function definition and the request here is to enforce function components instead of class components. It’s complementary but none overlapping.

tatethurston avatar Aug 05 '22 06:08 tatethurston

Does the function-component-definition rule meet everyone's use cases here?

No that rule enforces the function definition and the request here is to enforce function components instead of class components. It’s complementary but none overlapping.

Agree with Tate. This rule, while useful, doesn't solve the issue of preventing the introduction of class components into a codebase.

nebrius avatar Aug 05 '22 15:08 nebrius

Looks like this PR has gone dormant. I also support the addition of this rule. It would be useful when managing a codebase whose contributors have different levels of familiarity with React, e.g., a mix of FE specialists and mid-stack or backend devs that are more used to class-heavy OO languages.

Has anything changed from the maintainer's point of view over the last year? I feel like the greater community has almost fully abandoned classes.

robwierzbowski avatar Apr 03 '23 20:04 robwierzbowski

@robwierzbowski nope, nothing's changed there, and ErrorBoundary in particular still is required to be a class component.

ljharb avatar Apr 03 '23 20:04 ljharb

That's true. I believe this code allows error boundaries to remain class components. As you discussed above, whether this is hardcoded true or an option, the issue could be addressed.

robwierzbowski avatar Apr 03 '23 20:04 robwierzbowski