question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Seems to be trying to fetch from local storage before window is defined in React/Next SSR environment

See original GitHub issue

I’m using use-local-storage-stare in a custom hook to store a UUID in local storage. I’m calling my hook in several places in the app and I can see that when the window is undefined that I get different UUIDs from my default setting until window is defined and then I get the correct UUID from storage returned. Here is my implementation:

`import useLocalStorageState from “use-local-storage-state”; import { v4 as uuid } from “uuid”;

const LOCAL_STORAGE_KEY = “RESONANCE_DEVICE_ID”;

const useDeviceId = (): string | undefined => { const [deviceId] = useLocalStorageState<string>(LOCAL_STORAGE_KEY, { defaultValue: uuid(), });

return deviceId;

};

export default useDeviceId; `

and I’m on React 18.2.0, Next 12.2.2 , use-local-storage-state 18.1.0 logging on Server (note different id being generated by default UUID() since window is still undefined at this point (best guess). image

logging on client: can see that the id from storage comes through after first 2 defaults get created. image

Question, is there a way to disable until window is defined? An option something like { useSSR: false } or similar?

Thank you!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gDub74commented, Oct 4, 2022

Sorry for the trouble, I miss understood the purpose of your the use-local-storage-state hook w/ regards to SSR. I will make a small wrapper for my use case.

0reactions
astoilkovcommented, Oct 4, 2022

Do you have ideas of how this could be implemented inside of use-local-storage-state?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix "localStorage is not defined" in Next.js
This is because localStorage is not defined on the window object, and Next. js performs a server render before the client render.
Read more >
In React and Next.js constructor, I am getting "Reference Error
js is running on the server before accessing the localStorage . I.e., const ISSERVER = typeof window === "undefined"; if(!ISSERVER) ...
Read more >
Using localStorage with React Hooks - LogRocket Blog
Learn how using localStorage with React Hooks can persist user information in browser storage and share logic between multiple components.
Read more >
localStorage is not defined - Next.js · Discussion #19911
Hi @danilo-leal. The issue you are seeing is because localStorage (aka window.localStorage ) is not defined on the server side. Next server ...
Read more >
How to Save State to LocalStorage & Persist on Refresh with ...
Once it does, we're using localStorage's getItem method to grab the data associated with our key. If that data does not equal null...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found