Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kigiri/react-use-idb

React side-effect hook that manages a single indexDB item.
https://github.com/kigiri/react-use-idb

Last synced: about 1 month ago
JSON representation

React side-effect hook that manages a single indexDB item.

Awesome Lists containing this project

README

        

# react-use-idb (`useIdb`)

React side-effect hook that manages a single `indexDB` item.

A drop-in remplacement over `useLocalStorage`.

## Why ?

`LocalStorage` is synchronous and as such, has [performances issues](https://hacks.mozilla.org/2012/03/there-is-no-simple-solution-for-local-storage/)

`LocalStorage` is also limited, only storing strings and does not provide a default mechanisme for serializing / deserializing your data.

Instead, we can rely on `indexDB` for structural cloning.

## Usage

```jsx
import { useIdb } from 'react-use-idb'

const Demo = () => {
const [value, setValue] = useIdb('my-key', 'foo')

return (


Value: {value}

setValue('bar')}>bar
setValue('baz')}>baz

)
}
```

## Reference

```js
useIdb(key)
useIdb(key, initialValue)
```

- `key` — `indexDB` item key to manage.
- `initialValue` — initial value to set, if value in the `indexDB` item is empty.

> Inspired by [idb-keyval](https://github.com/jakearchibald/idb-keyval)