https://github.com/devhammed/use-cookie
Get, Set, Update and Delete Cookie using React Hooks.
https://github.com/devhammed/use-cookie
cookie react react-hooks
Last synced: 7 months ago
JSON representation
Get, Set, Update and Delete Cookie using React Hooks.
- Host: GitHub
- URL: https://github.com/devhammed/use-cookie
- Owner: devhammed
- Created: 2019-04-25T18:31:49.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-23T02:04:59.000Z (about 1 year ago)
- Last Synced: 2024-11-29T13:20:14.240Z (7 months ago)
- Topics: cookie, react, react-hooks
- Language: JavaScript
- Homepage: https://hammed.dev/use-cookie/
- Size: 1.21 MB
- Stars: 17
- Watchers: 3
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @devhammed/use-cookie
> Get, Set, Update and Delete Cookie using React Hooks.
[](https://www.npmjs.com/package/@devhammed/use-cookie) [](https://standardjs.com) [](https://github.com/acekyd/made-in-nigeria)
## Install
```bash
npm install --save @devhammed/use-cookie
```## Usage
```tsx
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 (
Hello {username}!
Edit below input, Your name will be stored in a cookie. you can refresh this page to see how it persists.
setUsername(e.target.value)}
/>
deleteUsername()}
>
Delete Cookie
)
}ReactDOM.render(, document.getElementById('root'))
```
[View Demo](https://devhammed.github.io/use-cookie)## API
```tsx
// 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](https://github.com/devHammed)