-
Notifications
You must be signed in to change notification settings - Fork 97
/
index.js
32 lines (28 loc) · 910 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { useState } from 'react';
import usePersistedState from './usePersistedState';
import createStorage from './createStorage';
const getProvider = () => {
if (typeof global !== 'undefined' && global.localStorage) {
return global.localStorage;
}
// eslint-disable-next-line no-undef
if (typeof globalThis !== 'undefined' && globalThis.localStorage) {
// eslint-disable-next-line no-undef
return globalThis.localStorage;
}
if (typeof window !== 'undefined' && window.localStorage) {
return window.localStorage;
}
if (typeof localStorage !== 'undefined') {
return localStorage;
}
return null;
};
const createPersistedState = (key, provider = getProvider()) => {
if (provider) {
const storage = createStorage(provider);
return (initialState) => usePersistedState(initialState, key, storage);
}
return useState;
};
export default createPersistedState;