Added rating system
This commit is contained in:
parent
18c6684fb2
commit
e9cb1d9d54
6 changed files with 101 additions and 20 deletions
30
src/hooks/useLocalStorage.jsx
Normal file
30
src/hooks/useLocalStorage.jsx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { useEffect, useState } from "react"
|
||||
|
||||
const useLocalStorage = (key, defaultValue) => {
|
||||
const [storedValue, setStoredValue] = useState(() => {
|
||||
try {
|
||||
const value = window.localStorage.getItem(key)
|
||||
|
||||
if (value) {
|
||||
return JSON.parse(value)
|
||||
} else {
|
||||
window.localStorage.setItem(key, JSON.stringify(defaultValue))
|
||||
return defaultValue
|
||||
}
|
||||
} catch (err) {
|
||||
return defaultValue
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
window.localStorage.setItem(key, JSON.stringify(storedValue))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}, [storedValue])
|
||||
|
||||
return [storedValue, setStoredValue]
|
||||
}
|
||||
|
||||
export default useLocalStorage
|
||||
Reference in a new issue