Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda