[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:
parent
43ae248e87
commit
58edba6983
1 changed files with 4 additions and 1 deletions
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue