@devhammed/use-cookie
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

@devhammed/use-cookie

Get, Set, Update and Delete Cookie using React Hooks.

NPM JavaScript Style Guide Made in Nigeria

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'))

View Demo

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

Readme

Keywords

none

Package Sidebar

Install

npm i @devhammed/use-cookie

Weekly Downloads

37

Version

1.1.1

License

MIT

Unpacked Size

10.2 kB

Total Files

5

Last publish

Collaborators

  • devhammed