1.1.2 • Published 6 years ago
@hacknlove/usestorage v1.1.2
usestorage
react state hook for using browser extension storage
Install
npm i @hacknlove/usestorageAPI
useStorage(key, first = null, area = 'sync')
keystring: the key to be retrievedfirstany the initial value to be returned before get the actual one.area'sync' or 'local' the storage to use.
returns [value, set] with the value in the storage at key and a function to update the key in the storage
preStore(keys, area = 'sync')
keyarray of the keys to be cachedareathe storage to use.
speed up useStorage caching the actual values for the first use.
Example
import React from 'react'
import ReactDOM from 'react-dom'
import useStorage, { preStore } from '@hacknlove/usestorage'
preStore(['world']) // optional optimization
function Example () {
const [world, setWorld] = useStorage('world')
return (
<h1>Hello {world}</h1>
<button onClick={() => setWorld('earth')}>World's name</button>
)
}
ReactDOM.render(
<Example/>,
document.querySelector('#root')
)