I was having the same problem with not being able to retrieve that data that was stored in localStorage in my simple React App. I could view the data in localStorage, but after refreshing the page, the data would be cleared. I found this solution after prompting Gemini for an answer,it solved the problem for me:
Strict Mode (React 18 and later): If you are using React 18 in development mode, Strict Mode might cause your useEffect to run twice, which can cause confusion. This is usually not a problem in production. Try commenting out <React.StrictMode> to see if it makes a difference.