I started analyzing useLocalStorage hook from https://usehooks-ts.com/ and I am wondering why they are actually allowing value to be a function in setValue ? There is a comment "Allow value to be a function so we have the same API as useState" but I don't really understand it. In what case value would be a function? Could anyone explain it for me?
try {
// Allow value to be a function so we have the same API as useState
const newValue = value instanceof Function ? value(storedValue) : value
// Save to local storage
window.localStorage.setItem(key, JSON.stringify(newValue))
// Save state
setStoredValue(newValue)