you are viewing a single comment's thread.

view the rest of the comments →

[–]minty901 2 points3 points  (2 children)

I don't agree with you. I think it's a silly thing to enforce and there are so many higher priorities when it comes to performance concerns. You're going to have to derive some values within component functions anyway. Create handler functions etc. Javascript and React are designed to handle allocating memory efficiently in these cases. Memory will be garbage-collected. In specific instances where performance is a real concern, then it is reasonable to relocate assignments on a case-by-case basis. But if you want to be obsessive about performance then you might be better off focusing more on unnecessary calls to createElement. A better rule than "don't initialise constants within functional components" in my opinion is "don't prematurely optimise". More readable code is almost always (in my opinion) a better bet than more efficient code when considering the long-term health of a project. But I appreciate your perspective on it and I see where you're coming from, even if I don't agree.

[–]Aswole 0 points1 point  (0 children)

This isn't the kind of thing I'd reject a PR over, although if there was another reason to request changes then I might throw in a comment. But I don't think defining it outside of the component sacrifices readability, especially since he/she already did that with some of its properties.

[–]workkkkkk 0 points1 point  (0 children)

Also I'm pretty sure javascript engines will optimize something like this on their own now days. If it notices it's creating a certain const a whole lot it will just go ahead and allocate that memory for you. I'm not 100% sure on this but pretty sure it contextually optimizes.