mirror of
https://github.com/oceanprotocol/market.git
synced 2024-11-15 09:44:53 +01:00
30 lines
664 B
TypeScript
30 lines
664 B
TypeScript
import { isBrowser } from '../utils'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
function useStoredValue<T>(
|
|
key: string,
|
|
initialState: T
|
|
): [T, (newValue: T) => void] {
|
|
const [value, setValue] = useState<T>(initialState)
|
|
|
|
useEffect(() => {
|
|
if (isBrowser) {
|
|
const storedValue = localStorage.getItem(key)
|
|
if (storedValue) {
|
|
setValue(JSON.parse(storedValue) as T)
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
const updateValue = (newValue: T) => {
|
|
if (isBrowser) {
|
|
localStorage.setItem(key, JSON.stringify(newValue))
|
|
}
|
|
setValue(JSON.parse(JSON.stringify(newValue)))
|
|
}
|
|
|
|
return [value, updateValue]
|
|
}
|
|
|
|
export default useStoredValue
|