diff options
author | eug-vs <eug-vs@keemail.me> | 2020-11-15 04:07:33 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-11-15 04:07:33 +0300 |
commit | 81b46581ab79f8f5e9e132e00e5d1b8e9182dd46 (patch) | |
tree | 7c46c3fd9b2b66af20ce91b61ac889d327c1e492 /src/hooks/useLocalStorage.ts | |
parent | eb2e929e2188ccfd9f575a5aa425024bf1fec67f (diff) | |
download | famcs-kit-81b46581ab79f8f5e9e132e00e5d1b8e9182dd46.tar.gz |
feat: implement authentication
Diffstat (limited to 'src/hooks/useLocalStorage.ts')
-rw-r--r-- | src/hooks/useLocalStorage.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/hooks/useLocalStorage.ts b/src/hooks/useLocalStorage.ts new file mode 100644 index 0000000..a604ebe --- /dev/null +++ b/src/hooks/useLocalStorage.ts @@ -0,0 +1,20 @@ +import { useState, useCallback } from 'react'; + +type Value = string | null; +type Setter = (value: Value) => void; + +const useLocalStorage = (key: string): [Value, Setter] => { + const [state, setState] = useState<Value>(() => localStorage.getItem(key) || null); + + const update: Setter = useCallback(value => { + if (value) localStorage.setItem(key, value); + else localStorage.removeItem(key); + setState(value); + }, [key]); + + return [state, update]; +}; + + +export default useLocalStorage; + |