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

164
Vistas
Set state twice in onChange ReactJS

I have the following snippet of React that I can't get to work right. Essentially I'm displaying a spinner based on the state of this onChange call (due to the update sometimes taking 3-5 seconds). However, the first call to set state seems to go unnoticed (or as I'm researching being batched up) and therefore the state of loading is never updated. This is part of a toggle/switch a user can select infinite times on a page (if they desired obviously).

  const [state, setState] = useState({ loading: false });  
  const onToggleChange = () => {
    setState(prevState => {
      return {
        ...prevState,
        loading: true,
      };
    });
    console.log("loading1: " + JSON.stringify(state));

    // this takes a long time depending on the users actions
    setFilterMode(!filterChecked);

    setState(prevState => {
      return {
        ...prevState,
        loading: false,
      };
    });
    console.log("loading2: " + JSON.stringify(state));
  };

I've reviewed the current questions/answer (which is how I got as far as the above), but it still doesn't load correctly.

about 4 years ago · Juan Pablo Isaza
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