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

216
Vistas
Checking two variables on condition from localStorage:

Is there a way to check to variable in localstorage ?

Storage {user: 'undefined', new: '{"_id":"61dd228336a3923d2286b994","email":"ahmadsa…","updatedAt":"2022-01-11T06:24:03.675Z","__v":0}', lscache-localhost:preview:v20180702-cacheexpiration: '27376332', lscache-localhost:preview:v20180702: '{}', length: 4}

And sometimes the data may be on the user property base on user login . below is the login that returns the property in user

   Storage {user: '{"team_members":{"memberFullname1":"memberFullname…","updatedAt":"2022-01-10T01:43:30.288Z","__v":0}', new: 'null', length: 2}

How can I create condition that checks both property an only return the ones that contain a value ?

Base on user log in, I can only use one variable either new or user. Is there not away I can check two conditions before rendering my data from localStorage ?

This is the Method I try. But it only check the first condition even though the second condition is there, it return. Uncaught error in Json.parse()

if (typeof Storage !== "undefined") {
  if (localStorage.getItem("new")) {
    setUser(JSON.parse(window.localStorage.getItem("new")));
  } else if (localStorage.getItem("user")) {
    setUser(JSON.parse(window.localStorage.getItem("user")));
  }
  return;
}

Is there a way I can check both condition ? Your time is really appreciated. please I am a brother need.. And lastly how can I prevent re-rendering of the state

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

0

If user undefined, do new, if that's undefined, do {}, parse.

if (typeof Storage !== "undefined") {
  setUser(JSON.parse(localStorage.user || localStorage.new || '{}'));
}

Though, if you look at new, it's not the same as user, in terms of structure so you might want to fix that else they are not the same thing.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found these code allows me to check both conditions and it didn't return error like the previous one.

   useEffect(() => {
    const fetchStates = async () => {
      const result = await axios(
        "https://nigerian-states-info.herokuapp.com/api/v1/states"
      );
      setStates(result.data.data);
    };
    fetchStates();
    if (localStorage.user === "undefined") {
      return setUser(JSON.parse(localStorage.getItem("new")));
    } else if (localStorage.new === "undefined") {
      return setUser(JSON.parse(localStorage.getItem("user")));
    }
  }, [formData, user]);

Finally after a bunch of try and error, I finally arrived at that part. I want to sent my sincere appreciation to all those who took there time and review my issues even those that did not get to answer. I send A BIG THANK YOU especially to lawrence cherone your idea was a great helpful in shaping my thought. thank you all

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