[WIP] Remove localStorage SSR error

Thankfully this wasn't a crasher since it was already in a try/catch, but it was logging a failure that didn't need to be logged! If `localStorage` isn't available (e.g. SSR), just use the initial value.
This commit is contained in:
Emi Matchu 2022-09-14 22:35:33 -07:00
parent 43ae248e87
commit 58edba6983

View file

@ -312,8 +312,11 @@ export function useFetch(url, { responseType, skip, ...fetchOptions }) {
let storageListeners = []; let storageListeners = [];
export function useLocalStorage(key, initialValue) { export function useLocalStorage(key, initialValue) {
const loadValue = React.useCallback(() => { const loadValue = React.useCallback(() => {
if (typeof localStorage === "undefined") {
return initialValue;
}
try { try {
const item = window.localStorage.getItem(key); const item = localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue; return item ? JSON.parse(item) : initialValue;
} catch (error) { } catch (error) {
console.error(error); console.error(error);