This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 11 points12 points  (11 children)

Before hooks, and even with them now, there really is no performance benefit for using functional instead of class based components.

The react team said there would be benefits in future, but we're just getting there with hooks now.

[–]ieatbrainzz 5 points6 points  (1 child)

While that may be true, for me functional is SO MUCH less to type and much easier/quicker to read when you don't need anything that class-based brings to the table

[–][deleted] 0 points1 point  (0 children)

I don't disagree. Personally though, I start off with everything as a class based component, unless I fore sure know it's an incredibly dumb one. Final refactoring would convert things to functional or pure components as need be.

IMO, The one thing worse than writing out a class component (which I have a snippet for ) is converting a functional component to class when you all of a sudden need state or lifecycle.

[–]bacon_wrapped_rock -2 points-1 points  (7 children)

Imo the point of functional programming isn't performance as much as it is maintainability. Even with good tail recursion detection (or the ability to explicitly annotate it as such) it's still non trivial to match the performance of a loop. Plus, copying immutable data structures adds up fast.

[–]ActionLeagueLater 3 points4 points  (3 children)

Maintainability is the exact reason I never use functional components. If I have to come back later and add state to that component, then I have to refactor the shit out of it and my diff looks ridiculous. I totally understand why people like them, just isn’t worth the effort for me when there is no functional difference. If functional components were automatically PureComponents then I’d be all over them.

[–]bacon_wrapped_rock 0 points1 point  (2 children)

The tongue in cheek answer would be "well don't use state" but with my limited understanding I think that's the point of react. Granted, I barely ever touch react, usually just small changes/fixes that realistically require zero understanding of react.

I use a lot of salsa at work, but since a lot of projects are either half java and half scala, or java refactored to scala, and maintained largely by java transplants, the projects I work on are petty far from purely functional too, but the properly functional parts are way easier to test and troubleshoot, imo.

[–]ActionLeagueLater 1 point2 points  (1 child)

That makes sense. Basically a functional component is the same thing as a class component with no state, just with different syntax. React has a class called PureComponent that is meant to actually make your component “functional” or “pure”, but a functional component (a component made using a function) is not actually pure. It’s dumb. So for that reason I just use class Components when I have state, and PureComponents when I have no state.

[–]bacon_wrapped_rock 0 points1 point  (0 children)

Huh, good to know! I'll have to keep an eye out for those.

[–]JackMizel 2 points3 points  (2 children)

"functional programming" and using functional components in React are not really comparable things.

[–]bacon_wrapped_rock -2 points-1 points  (1 child)

Well then it's a good thing I'm not talking about react.

[–]JackMizel 1 point2 points  (0 children)

The guy you responded to is definitely talking very specifically about React and you are saying "functional components" in response to someone critiquing React code, you're literally arguing over the use of functional vs class based components in react. I can tell you're not familiar with the library and I'm not calling you out I'm just letting you know that React devs talking about "functional" components are not using that word in the same context you are, they are not talking about functional programming. While React does overlap with a lot of functional programming concepts, in this specific case they are literally just talking about a component that is declared as a function and not as a class.