use localStorage
w-gasiewicz opened this issue · comments
I have problem to build react-static app when I use localStorage. Example below:
const storedShow = Number(localStorage.getItem("show"));
const [show, setShow] = useState(1);
useEffect(() => {
localStorage.setItem("show", String(show))
}, [show])
App works perfectly with npm start on localhost. But when I try to make build I get errors like:
ReferenceError: Failed exporting HTML for URL....
When I remove localStorage from code build works fine. Is it posiible to build react-static app with localStorage?
Are you accessing localStorage
when typeof window === 'undefined'
? If yes, don't. Add a guard clause around it:
if (typeof window !== 'undefined') {
// use localstorage here
}
// Or:
const myValue = typeof window === 'undefined' ? "some default value" : localStorage.get(...)