Storage
/ API ReferenceStorage
/ API ReferenceuseIndexedStorage
A React hook to manage a value in IndexedDB.
It uses useStorage and createIndexedStorage under the hood.
The Gist
import { useIndexedStorage } from '@weser/storage'
function Component() {
const [counter, setCounter, isLoading] = useIndexedStorage(
'my-database',
'my-table',
'counter',
0
)
if (isLoading) {
return <div>Loading...</div>
}
return (
<div>
{counter}
<button onClick={() => setCounter(counter + 1)}>Increment</button>
<button onClick={() => setCounter(counter - 1)}>Decrement</button>
</div>
)
}
Parameters
| Parameter | Type | Description |
|---|---|---|
| database | string | The name of the database to use. |
| table | string | The name of the table to use. |
| key | string | The key of the item to set. |
| initialState | T | The initial value of the item. |
| config | Config | The configuration object. |
Returns
([T, Dispatch<SetStateAction<T>>, boolean]) A tuple where the first element is the value, the second element is a function to update the value and the third element is a boolean indicating if the value is still loading.
© 2024-present Robin Weser. All Rights Reserved.