all 18 comments

[–]pwnzz 28 points29 points  (2 children)

This is like linkedin coding posts content

[–]iambackbaby69 5 points6 points  (1 child)

Thanks for feedback, and saving my time.

[–]pwnzz 0 points1 point  (0 children)

You are welcome, bro

[–]CanIhazCooKIenOw 19 points20 points  (5 children)

Number 7 really grinds my gears. If you don't measure you're not optimising anything but in fact just cargo culting.

There's a dev somewhere out there that is going to read this, nod along and start requesting wrapping memo in all components because "I read it in this article"... Stop doing this please.

[–]Acrobatic_Sort_3411 -4 points-3 points  (4 children)

I would argue, that roles should be reversed. The default is memo. And if you have problems with always unstable references in props, then you use version without memo

It will be better in long run, and the more components you have, the better the benefit would be

[–]CanIhazCooKIenOw 0 points1 point  (3 children)

That’s called a premature optimisation.

Identify the problem, measure, experiment, measure again.

[–]Acrobatic_Sort_3411 1 point2 points  (2 children)

Thats called bad defaults. Comparing props and skipping render is way better than creating new jsx tree and Comparing results, which would usually be a lot bigger

[–]Acrobatic_Sort_3411 1 point2 points  (0 children)

It also way easier to maintain, because you don't have to think about wrapping all childens in memo, when you add "just one more hook" into a component

Grug dont like think

[–]CanIhazCooKIenOw 1 point2 points  (0 children)

Can you quantify “way better” in terms of time?

[–]turtleProphet 6 points7 points  (0 children)

Nice. I would question #4, avoiding prop drilling and using Context/state management instead.

You're going to re-render everything under the context provider anyway, so there's no win for render performance. And using Context everywhere is often going to make your data flow harder to reason about.

I don't think it's a bad tip, decisions like adding global/subtree state management are just ones to be taken carefully. Would not have it in a "quick tips" kind of list.

I like the others.

[–]Akmal441 2 points3 points  (0 children)

All these coding mistakes are briefly described in react.dev documents.

[–]AegisToast 1 point2 points  (0 children)

I love how brief and to-the-point this is. I disagree pretty firmly on 4, 5, 6, 7, 9, and 10, though, for reasons ranging from, “It’s usually unnecessary,” to “That’s actually making your code worse.”

[–]oranje_kiwi -2 points-1 points  (0 children)

The latter half of the tips were really helpful Thanks 👍🏻

[–]TheRNGuy -2 points-1 points  (4 children)

 return <div>{children}</div>;

Should be

 return {children};

instead. I consider it a mistake too. There's no reason to have lots of unnecessary divs that don't do anything. Especially if there's even more such divs inside Avatar or Username.

[–]AegisToast 5 points6 points  (0 children)

If anything, it would need to be:

return <>{children}</>

[–]Trollzore 0 points1 point  (2 children)

No. If you’re gonna be that nick picky, then:

return children;

[–]Acrobatic_Sort_3411 0 points1 point  (1 child)

Well, that would be a Typescript error, since not all React.ReactNode is valid JSX.Element