News for hackers. Stuff that matters.
Re:Understanding UseMemo and UseCallback by acover on Wednesday August 31, 2022 @12:00AM (#32658238)
Isn't useCallback to solve the common mistake of:

<button onClick={() => setText("clicked")}>click</button>

Without useCallback this would update the dom on every render.

Re:Understanding UseMemo and UseCallback by littlecranky67 on Wednesday August 31, 2022 @03:13AM (#32659293)
Unfortunately the "update on every render" is deeply anchored into devs minds, although in most cases play a minor role. It comes back from a time where browser JS engines where really slow to create lambdas - which is not the case anymore. Additionally, react doesn't really update the DOM on "render" (the internal react render cycle) - due to DOM diffing.

My best advice is (also apart from React): Do not spend time on imagined/anticipated performance bottlenecks; only ever spend time on performance tuning if there are real (perceived, not measurable!) performance issues (there has to be a profiling report in your hand).

Re:Understanding UseMemo and UseCallback by acover on Wednesday August 31, 2022 @12:11PM (#32664269)
Yeah I only started caring about this when tracking down lag in my app. Though it's fun to think about :)

I believe this causes a real Dom update because it can't know the onclick function hasn't materially changed. It's just diffing function references.

Re:Understanding UseMemo and UseCallback by graftak on Wednesday August 31, 2022 @03:50AM (#32659513)
The main use for useCallback is to add a callback to a useEffect dependency array without it running on every render.
Re:Understanding UseMemo and UseCallback by darepublic on Wednesday August 31, 2022 @08:53AM (#32661435)
That's not a mistake lol.
Re:Understanding UseMemo and UseCallback by acover on Wednesday August 31, 2022 @12:04PM (#32664167)
Can you explain why it isn't? I'm probably misunderstanding something.

setText doesn't change because it's from useState. The only reason to update the Dom is because the onclick function isn't memoized.

Updating the Dom every render isn't terrible but it isn't good.

Re:Understanding UseMemo and UseCallback by darepublic on Saturday September 03, 2022 @10:59PM (#32709340)
button is just a small lightweight element usually with a single string child. If passing a lightweight unwrapped click handler to a leaf node is a faux pas then honestly the react team needs to memoize by default. Your description "updating the dom every render..." And acting like indiscriminate useCallback is a panacea for your performance problems to me seems like a pretty naive take. But then again seems like the community opinion in this has shifted, I just honestly don't get it. And I do use useCallback btw, but only when I need to keep referential stability for known reasons,not as an automatic default