Skip to content

Commit

Permalink
@mantine/hooks] use-local-storage: Fix error for environments where l…
Browse files Browse the repository at this point in the history
…ocalStorage can be null (e.g. Android WebView) (#2613)

By default, `window.localStorage` is set to null in Android WebViews
unless the embedding app has explicitly enabled storage, leading to an
uncaught exception in this hook. Fix it by checking for null and
returning the default value.
  • Loading branch information
kdrag0n committed Oct 3, 2022
1 parent 2f1773e commit d8115fb
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/mantine-hooks/src/use-local-storage/create-storage.ts
Expand Up @@ -48,7 +48,12 @@ export function createStorage<T>(type: StorageType, hookName: string) {
}: IStorageProperties<T>) {
const readStorageValue = useCallback(
(skipStorage?: boolean): T => {
if (typeof window === 'undefined' || !(type in window) || skipStorage) {
if (
typeof window === 'undefined' ||
!(type in window) ||
window[type] === null ||
skipStorage
) {
return (defaultValue ?? '') as T;
}

Expand Down

0 comments on commit d8115fb

Please sign in to comment.