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

103
Vistas
React can the callback function of useState increase performance?

With Callback

  const [state, setState] = useState(() => {
    email: '',
    password: '',
  });

  const handleStateChange = useCallback((stateName) => (e) => {
    setState((prevState) => ({
      ...prevState,
      [stateName]: e.target.value,
    }));
  }, []);

Without Callback

const [state, setState] = useState(() => {
    email: '',
    password: '',
  });

  const handleStateChange = useCallback((stateName) => (e) => {
    setState({
      ...state,
      [stateName]: e.target.value,
    });
  }, [state]);

It's possible to use dispatch of useState without appending dependencies through the callback function.

I think it reduces dependencies so that helps for performance.

Is this correct?

In addition, sometimes, the callback argument doesn't get current state correctly. I don't know why it doesn't work.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There likely isn't a measurable difference between the two approaches (but you could try testing it!). This is extreme early optimization, and if you want to increase performance, you should focus on other parts of your app that can yield better results.

The difference between the two approaches is to solve different problems (getting the prevState vs not getting It). It calls the same logic internally, and React should be smart enough to not create a performance difference between the two.

Think of it this way: do you worry about adding an extra state or two because of performance? No, of course not, because it doesn't matter.

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