From 58edba6983e8e99730f363af14b8027fbffd62a8 Mon Sep 17 00:00:00 2001 From: Matchu Date: Wed, 14 Sep 2022 22:35:33 -0700 Subject: [PATCH] [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. --- src/app/util.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/app/util.js b/src/app/util.js index 58606f64..b2cb2214 100644 --- a/src/app/util.js +++ b/src/app/util.js @@ -312,8 +312,11 @@ export function useFetch(url, { responseType, skip, ...fetchOptions }) { let storageListeners = []; export function useLocalStorage(key, initialValue) { const loadValue = React.useCallback(() => { + if (typeof localStorage === "undefined") { + return initialValue; + } try { - const item = window.localStorage.getItem(key); + const item = localStorage.getItem(key); return item ? JSON.parse(item) : initialValue; } catch (error) { console.error(error);