Add vendors listing with search, category filter, grid/list, pagination
This commit is contained in:
24
frontend/src/hooks/use-local-storage.ts
Normal file
24
frontend/src/hooks/use-local-storage.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
export function useLocalStorage<T>(key: string, initialValue: T): [T, (v: T) => void] {
|
||||
const [value, setValue] = useState<T>(() => {
|
||||
if (typeof window === 'undefined') return initialValue
|
||||
try {
|
||||
const raw = window.localStorage.getItem(key)
|
||||
return raw != null ? (JSON.parse(raw) as T) : initialValue
|
||||
} catch {
|
||||
return initialValue
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
window.localStorage.setItem(key, JSON.stringify(value))
|
||||
} catch {
|
||||
/* storage unavailable */
|
||||
}
|
||||
}, [key, value])
|
||||
|
||||
const set = useCallback((v: T) => setValue(v), [])
|
||||
return [value, set]
|
||||
}
|
||||
Reference in New Issue
Block a user