Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda