Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Is there a performance/usability benefit to using useCallback for InputElement Events?

When not passing a callback to a child and just using it on the present component, Is there a benefit to wraping the callback in a useCallback? This:

 const Foo = (
  const [count, setCount] = useState(500);

  const onChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    setCount(Number(e.target.value));
  }, [setCount]);

  return (
    <>
      <div>
        Delay: <input value={count} onChange={onChange} type="number" />
      </div>
    </>
  );

Verse This:

 const Foo = (
  const [count, setCount] = useState(500);

  const onChange =(e: React.ChangeEvent<HTMLInputElement>) => {
    setCount(Number(e.target.value));
  };

  return (
    <>
      <div>
        Delay: <input value={count} onChange={onChange} type="number" />
      </div>
    </>
  );

Codesandbox: https://codesandbox.io/s/loving-stonebraker-48xhs?file=/src/Counter.tsx

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I see no benefit there when used internally since each state update (setCount(Number(e.target.value));) necessarily rerenders the component anyway and all the callback is doing is enqueueing a state update.

There may be a small (very negligible) improvement in memory usage if using a single declared, memoized function for the life of the component.

If the callback was used in an useEffect hook it could be provided as a stable reference and be removed from dependency arrays, but typically here you'd just move the function into the effect callback. This doesn't fit the use case you are asking about, but it's a valid use case that isn't passing callbacks down to children.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!