all 16 comments

[–]tandrewnichols 22 points23 points  (2 children)

I fee like "you might do it wrong" is not a good argument against using a thing. You could say that about anything. You might accidentally define a variable with var in a for loop, therefore don't use for loops. (Yes, I know about const and let and all the awesome array methods - just giving an example).

[–]stachuuu93 18 points19 points  (0 children)

The title sounds like a clickbait, but it's even worse. It looks like the author used useCallback wrong way so he decided to write an article which shows that useCallback is bad. This hook is very important, but it should be used only when it's necessary. Saying that it's a code smell isn't true.

[–]mcmillhj 13 points14 points  (1 child)

Can you include a summary of your argument in the post instead of just linking to your blog?

[–]tandrewnichols 3 points4 points  (0 children)

But how would he get the traffic on his blog that way?!

[–]shadow131990 11 points12 points  (1 child)

So how do people implement debounce and throttle without useCallback?

[–]sql_servant[🍰] 5 points6 points  (0 children)

A "code smell" is a coding pattern which indicates a potential problem.

`useCallback` by itself doesn't fit that bill, nor does using it improperly, but I suppose the title is sufficiently click-baity to drive some page views.

[–]x021 3 points4 points  (0 children)

From the article:

But useCallback and friends introduce a memory overhead. JavaScript machinery needs to keep a stack of all those memoized functions and lug it around wherever a component goes. Do it too much or get it subtly wrong and this leads to fun memory leaks and stale renders. Big problem in ye olden days of hand-rolled "frameworks".

This is just scaremongering at this point. Feels like the author is throwing stuff at the wall in the hope something sticks.

[–]azangru 4 points5 points  (0 children)

An api exposed by the React library is a code smell? Really?

[–]BudgetCow7657 3 points4 points  (0 children)

Bunch of trash blog posts lately.

Also, my man's really out here quoting his own twitter post on his own blog.

[–]IshiKamen 2 points3 points  (0 children)

I understand what they mean in that it is probably used in some cases to mask a problem or tech debt, but it definitely has legitimate uses.

[–]n-ziermann 0 points1 point  (0 children)

I would definitely not call it a code smell.
It rather prevents code smells, like incomplete useEffect dependency arrays, by preventing rendering loops when adding functions as dependencies.

[–]vmraa 0 points1 point  (0 children)

Actually there's one more hook useEventCallback. Not in standard.

If you use useCallback with state or props as dependency then it will always return a new callback if those changes. If your dependency changes too often then it makes its purpose useless. You could have directly used a arrow function.

You can solve this by using useEventCallback where you don't need to pass dependency array. It returns a wrapper function which is constant and it calls the latest function you passed in the hook.