4 ms·
Functions are stateless and I believe can be better optimized. Enforcing statelessness is also a win in itself.
by cozos 8y ago
Functions are stateless and I believe can be better optimized. Enforcing statelessness is also a win in itself.
- RoryH 8y agoIndeed, also checkout the React.PureComponent for another example of an in-between Class and Function optimisation.
- quietbritishjim 8y agoSurprisingly, 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!).
- peterjmag 8y agoI like function components too, but I've seen some interesting arguments against using them: https://medium.freecodecamp.org/7-reasons-to-outlaw-reacts-functional-components-ff5b5ae09b7c 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-class-to-function 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.
- k__ 8y agoSadly (or luckily?) with hooks they aren't stateless anymore.
- city41 8y agoThis is true in theory but the React team has not optimized them directly. React.memo is the only way to optimize them thus far.
- orf 8y agoI think he's referring to the optimizations the JIT/engine will do.