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

1.2K
Visualizações
How to check if localStorage is empty or not in React js?

So I'm trying to check if localStorage is empty or not and if it's then I want my state to be changed immediately.

I've created this state and I'm setting this state initially empty

const [isLoggedIn, setLogin] = useState();

Then I'm using useEffect hook to get item from localStorage if something has in localStorage it will set my state true otherwise false

useEffect(() => {
  if (localStorage.getItem("info") !== null) {
    setLogin(true);
  } else {
    setLogin(false);
  }
}, []);

So, when I add something or delete from my localStorage it doesn't work and I need refresh my page in order to make it work.

I've created a code sandbox to check what's going on, please have a look and help to make it work.

https://codesandbox.io/s/great-mclaren-vypyo?file=/src/App.js:164-304

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your localStorage commands look correct. But the issue is that you are not checking the storage everytime. You should have a state variable which you can use to indicate that something has changed and the storage needs to be checked again.

import "./styles.css";
import React, { useState, useRef, useEffect } from "react";

export default function App() {
  const [isLoggedIn, setLogin] = useState();
  const [change, setChange] = useState(false);

  useEffect(() => {
    if (localStorage.getItem("info") !== null) {
      setLogin(true);
    } else {
      setLogin(false);
    }
  }, [change]);

  console.log(isLoggedIn);

  return (
    <div className="App">
      {isLoggedIn ? (
        <h1>LocalStorage has something</h1>
      ) : (
        <h2>LocalStorage has nothing</h2>
      )}

      <button
        onClick={() => {
          localStorage.setItem("info", "true");
          setChange((change) => !change);
        }}
      >
        Add to Local
      </button>
      <button
        onClick={() => {
          localStorage.removeItem("info");
          setChange((change) => !change);
        }}
      >
        Remove to Local
      </button>
    </div>
  );
}

In the above code, change is changed when one of your storage command runs. You could have had a counter or anything else too. The main purpose is to check the storage everytime you are chaning stuff. This way you can have multiple storage commands (even more than 2), and then you can check your storage after every change.

link

Another simple approach could be to simply set the logged in state inside your functions where you run localStorage commands :

import "./styles.css";
import React, { useState, useRef, useEffect } from "react";

export default function App() {
  const [isLoggedIn, setLogin] = useState();

  useEffect(() => {
    if (localStorage.getItem("info") !== null) {
    setLogin(true);
    } else {
      setLogin(false);
    }
  }, [isLoggedIn]);

  console.log(isLoggedIn);

  return (
    <div className="App">
      {isLoggedIn ? (
        <h1>LocalStorage has something</h1>
      ) : (
        <h2>LocalStorage has nothing</h2>
      )}

      <button
        onClick={() => {
          localStorage.setItem("info", "true");
          setLogin(true);
        }}
      >
        Add to Local
      </button>
      <button
        onClick={() => {
          localStorage.removeItem("info");
          setLogin(false);
        }}
      >
        Remove to Local
      </button>
    </div>
  );
}

Now you have one lesser state variable, but you will be directly manipulating the logged in state from your buttons.

link

about 4 years ago · Juan Pablo Isaza Relatório

0

If you don't setItem("info") I mean If "info" does not exist in localStorage then you'll get "null" and your code will work. But If you set something to "info" in localStorage for example if you do localStorage.setItem(""), then you won't get null, you'll get empty string. Therefore your code should be:

useEffect(() => {
if (localStorage.getItem("info") !== null || localStorage.getItem("info") !== "") {
  setLogin(true);
} else {
  setLogin(false);
}
}, []);

if you use array instead of string in "info" then

useEffect(() => {
if (localStorage.getItem("info") !== null || JSON.parse(localStorage.getItem("info")).length > 0) {
  setLogin(true);
} else {
  setLogin(false);
}
}, []);
about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is that on the load of the page, you are setting the existence/non-existence of the localStorage item in the state of the component. However, when an action (setItem/removeItem) is performed, you are not updating the state. The simple way to accomplish it is to update the state in the event listener itself.

<button onClick={() => {
    localStorage.setItem("info", "true");
    setLogin(true);
}}>Add to Local</button>

<button onClick={() => {
    localStorage.removeItem("info");
    setLogin(false);
}}>Remove to Local</button>

Here is an updated, forked sandbox link.

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