@devhammed/use-cookie
v1.1.1
Published
Get, Set, Update and Delete Cookie using React Hook.
Downloads
77
Readme
@devhammed/use-cookie
Get, Set, Update and Delete Cookie using React Hooks.
Install
npm install --save @devhammed/use-cookie
Usage
import React from 'react'
import ReactDOM from 'react-dom'
import useCookie from '@devhammed/use-cookie'
const App = () => {
const [username, setUsername, deleteUsername] = useCookie('username', 'User')
return (
<section>
<h1>Hello {username}!</h1>
<p>Edit below input, Your name will be stored in a cookie. you can refresh this page to see how it persists.</p>
<input
type='text'
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<button
onClick={() => deleteUsername()}
>
Delete Cookie
</button>
</section>
)
}
ReactDOM.render(<App />, document.getElementById('root'))
API
// Using the Hook
useCookie(key: string, defaultValue?: any): [any, (value: any, options?: object) => void, () => void]
// Setting Example
// Array or Object passed to the function will be turned into JSON.
// Options and the keys are optionally and below are the default options:
const options = {
expires: 0,
domain: '',
path: '',
secure: false,
httpOnly: false,
maxAge: 0,
sameSite: ''
}
setCookie(value: any, options?: object)
// Delete Example
deleteCookie()
useCookie() hook returns an array containing three items. The first item is the cookie value, the second item is the function to update the cookie and the third item is the function to delete the cookie.
License
MIT © devHammed