This is a React hook to help you use IndexedDB.
import useIdb from "use-idb-keyval";
const Demo = () => {
const [value, setValue, resetValue] = useIdb("count", 0);
return (
<>
<p>Count: {value}.</p>
<button onClick={() => setValue(previousValue => previousValue + 1)}>Increment</button>
<button onClick={resetValue}>Reset</button>
</>
);
};
When using the setter function, you can either pass a new value or a function that takes the previous value.
useIdb(key);
useIdb(key, initialValue);
useIdb(key, initialValue, (inititalValue) => { doSomething(initialValue});
key
—indexDB
item key to registerinitialValue
— initial value to set, if value in theindexDB
item is empty.- You can pass a function as the third parameter to do a computation with the initial value.
Inspired by idb-keyval