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

222
Vistas
why my localstorage is lagged and doesn't catch up?(react.js)

I'm developing my own project and have a simple problem which I can not solve easily.

So, I have a state like below

     const [state, setState] = useState({
        externalEvents: [],
      });

and I can update a state with a function like this

(state is saved in a localstorage too as you can see)

const addTodo = (e) => {
    e.preventDefault();
    if (title === "" || color === "") {
      alert("please type what to do or choose a color");
    } else {
      setState({
        externalEvents: [
          ...state.externalEvents,
          { title: title, color: color, custom: custom, id: uuidv4(), },
        ],
      },
      );
      localStorage.setItem('externalEvents',JSON.stringify(state.externalEvents))
      
     
    
      setTitle("");
      setCustom("");
    }
  };

but problem is my localstorage is delayed.

For instance. if I save 'A' to state, state woulbe like this

state:{externalEvents:[{A}]}

but localstorage only return empty array

localstorage

key              value
   externalEvents   []

so I added one more thing 'B' and it would be like this

state:{externalEvents:[{A},{B}]}

localstorage

  key              value
    externalEvents   [A]

it only update the past one and not the current one

Could you tell me why is my localstorage being dumb and lagged?(maybe I'm dumb) and also how can I solve this ridiculous? thx for reading, your help will be appreciated.

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