Get, Set, Update and Delete Cookie using React Hooks.
npm install --save @devhammed/use-cookieimportReactfrom'react'importReactDOMfrom'react-dom'importuseCookiefrom'@devhammed/use-cookie'constApp=()=>{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><inputtype='text'value={username}onChange={(e)=>setUsername(e.target.value)}/><buttononClick={()=>deleteUsername()}>
Delete Cookie
</button></section>)}ReactDOM.render(<App/>,document.getElementById('root'))// Using the HookuseCookie(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:constoptions={expires: 0,domain: '',path: '',secure: false,httpOnly: false,maxAge: 0,sameSite: ''}setCookie(value: any,options?: object)// Delete ExampledeleteCookie()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.
MIT © devHammed