V
V
Vann Damm2020-06-10 16:13:23
JavaScript
Vann Damm, 2020-06-10 16:13:23

Why does LocalStorage.getItem return a json object before writing data to it?

There is an effect that fires when the page loads, already has some information, which does not allow me to set the conditions. Why is this happening?
useEffect(()=>{
const localStorageData = localStorage.getItem(LocalStorageName);
console.log('without auth',localStorageData)
console.log('without auth',typeof(localStorageData));
},[])
5ee0dc712d219571839359.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2020-06-10
@effect_tw

That doesn't happen. If the storage locale is really empty, then such a query will return null. In your case, most likely there is already an entry under this name. This can be easily checked:
localStorage.getItem('test'); // null

localStorage.setItem('foo', 'bar')
localStorage.length // 1
localStorage.foo // bar
localStorage.getItem('foo') // bar

Then you can always open the developer console and see its contents.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question