News for hackers. Stuff that matters.
Re:How Does React Tell a Class from a Function? by cozos on Monday December 03, 2018 @04:16AM (#18587394)
Functions are stateless and I believe can be better optimized. Enforcing statelessness is also a win in itself.
Re:How Does React Tell a Class from a Function? by peterjmag on Monday December 03, 2018 @05:23AM (#18587694)
I like function components too, but I've seen some interesting arguments against using them:

https://medium.freecodecamp.org/7-reasons-to-outlaw-reacts-f...

And I wish I had a reference for this, but I seem to remember reading that any future performance optimizations that could be applied to function components could probably also be applied to class components (at least ones that contain only a render() function and nothing else).

In any case, I'm hoping that React eventually gets to a point where developers don't have to make a choice between class and function components, and the compiler or the runtime makes that decision for us on a per-component basis.

EDIT: As an example, here's a Babel plugin that takes care of the conversion for you at compile time: https://github.com/remcohaszing/babel-plugin-transform-react.... Not sure what its heuristics are for determining what should and shouldn't be converted though, if any.

Re:How Does React Tell a Class from a Function? by city41 on Monday December 03, 2018 @10:12AM (#18589333)
This is true in theory but the React team has not optimized them directly. React.memo is the only way to optimize them thus far.
Re:How Does React Tell a Class from a Function? by orf on Monday December 03, 2018 @10:17AM (#18589367)
I think he's referring to the optimizations the JIT/engine will do.
Re:How Does React Tell a Class from a Function? by RoryH on Monday December 03, 2018 @04:21AM (#18587408)
Indeed, also checkout the React.PureComponent for another example of an in-between Class and Function optimisation.
Re:How Does React Tell a Class from a Function? by quietbritishjim on Monday December 03, 2018 @04:46AM (#18587528)
Surprisingly, pure functional components are called even when their props haven't changed so PureComponent is not "in between" but "even more" than functional components. As for regular components, you can override this behaviour by adding a componentWillReceiveProps() method (yes, you can add a method to your functional component!).
Re:How Does React Tell a Class from a Function? by k__ on Monday December 03, 2018 @08:20AM (#18588523)
Sadly (or luckily?) with hooks they aren't stateless anymore.