Michael Landis
Oct 14, 2023

Premature optimization is the root of all evil, and this is a premature optimization.

Javascript engines have been optimizing for function creation and execution for over 20 years. All this does is make your code harder to read for < 1ms gains, offset by the cost of calling useCallback on every render.

The _only_ time useCallback should be used is if you need referential stability in an effect/memo/callback dependency array, or if you _discover_ that a component needs that integrity. (Most UI libraries manage incoming function properties so that they don't need to be memoized.)

DOM elements never need memoized callbacks. If you're seeing a slowdown from inlining functions on a div, you've got larger architectural considerations to worry about.

Michael Landis
Michael Landis

Written by Michael Landis

Front-end web developer, React enthusiast, vagabond.

Responses (1)