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

119
Vistas
Is this the correct way to update a state based on the previous state in react?

Here's my code:

  const [val, setVal] = useState(0);

  function onClick() {
    setVal(val + 1);
  }

I just wanted to check if this is the correct way to update a state based on a previous state? Why/why not?

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

0

This is a perfectly fine approach. Note that you could also get the value of your current state as a callback parameter on setVal, e.g.: setVal(prevState => prevState + 1).

Just be careful when manipulating arrays or objects since they shouldn't be directly mutated (with methods such as push or pop etc).

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