Skip to content

kigiri/react-use-idb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

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

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

  return (
    <div>
      <div>Value: {value}</div>
      <button onClick={() => setValue('bar')}>bar</button>
      <button onClick={() => setValue('baz')}>baz</button>
    </div>
  )
}

Reference

useIdb(key)
useIdb(key, initialValue)
  • keyindexDB item key to manage.
  • initialValue — initial value to set, if value in the indexDB item is empty.

Inspired by idb-keyval

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published