Categories
Uncategorised

Inline func vs useCallback

Here’s an example that compares inline functions with functions using useCallback. Many people believe that the hook is faster, but that’s not the case when you only have a simple, basic, or light function. So, don’t waste your time on these.

function ComponentWithInlineFunction() {
  const clickMe = evt => evt.preventDefault();
  return React.createElement('button', {onClick: clickMe}, 'Click me!');
}

function ComponentWithUseCallback() {
  const clickMe = React.useCallback(evt => evt.preventDefault(), []);
  return React.createElement('button', {onClick: clickMe}, 'Click me!');
}


Benchmark: React useCallback hook vs inline function – MeasureThat.net