1
0
Fork 0
freesewing/sites/shared/hooks/useLocalStorage.mjs

39 lines
1.3 KiB
JavaScript
Raw Normal View History

import { useState, useEffect } from 'react'
const prefix = 'fs_'
2021-12-11 14:04:05 +01:00
// See: https://usehooks.com/useLocalStorage/
export function useLocalStorage(key, initialValue) {
2022-06-06 13:20:24 -05:00
// use this to track whether it's mounted. useful for doing other effects outside this hook
// and for making sure we don't write the initial value over the current value
const [ready, setReady] = useState(false)
// State to store our value
const [storedValue, setValue] = useState(initialValue)
// set to localstorage every time the storedValue changes
// we do it this way instead of a callback because
// getting the current state inside `useCallback` didn't seem to be working
useEffect(() => {
if (ready) {
window.localStorage.setItem(prefix + key, JSON.stringify(storedValue))
2021-12-11 14:04:05 +01:00
}
}, [storedValue, key, ready])
2021-12-11 14:04:05 +01:00
// read from local storage on mount
useEffect(() => {
try {
// Get from local storage by key
const item = window.localStorage.getItem(prefix + key)
// Parse stored json or if none return initialValue
const valToSet = item ? JSON.parse(item) : initialValue
setValue(valToSet)
setReady(true)
} catch (error) {
console.log(error)
}
}, [setReady, setValue, key, initialValue])
return [storedValue, setValue, ready]
2021-12-11 14:04:05 +01:00
}