use-stor
v1.2.0
Published
A Custom Hook for working with local/session Storages and Cookies easily and effectively
Downloads
3
Maintainers
Readme
useStor
useStor is React custom hook library for managing browser storage
Installation
npm install use-stor
Usage
import useStor from 'use-stor'
export default App = () => {
const { local } = useStor()
const handleClick = () => {
local.set('myKey', 'hello world')
}
return (
<div>
<h1>Click the button to set 'Hello World' to localStorage!</h1>
<button onClick={handleClick}>Click Me!</button>
</div>
)
}
Methods
const { local, session } = useStor()
local.set('myKey', 'myValue') // sets 'myValue' to 'myKey'
local.get('myKey') // gets value of 'myKey'
local.remove('myKey') // removes 'myKey'
local.clear() // removes all localStorage
session.set('myKey', 'myValue') // sets 'myValue' to 'myKey'
session.get('myKey') // gets value of 'myKey'
session.remove('myKey') // removes 'myKey'
session.clear() // removes all sessionStorage
// EXP:
const tenMinutesInMilliseconds = 1000 * 60 * 10
local.set('myKey', 'myValue', tenMinutesInMilliseconds) // exp in milliseconds
local.get('myKey', true) // if has exp, pass true as second param.