wkkkis-hooks

use-cookie (use-cookie)

Manages cookies with declarative methods.

v0.1.0

Usage

import { use-cookie } from "@/hooks"
1"use client"; 2 3import React, { useState } from "react"; 4 5import { Button } from "@/components/ui/button"; 6import { 7 Card, 8 CardContent, 9 CardDescription, 10 CardFooter, 11 CardHeader, 12 CardTitle, 13} from "@/components/ui/card"; 14import { Input } from "@/components/ui/input"; 15 16import { useCookie } from "@/components/hooks/use-cookie"; 17 18export function UseCookieDemo() { 19 // useCookie hook for a demo cookie 20 const [cookieValue, setCookie, removeCookie] = useCookie<string>( 21 "demo-cookie", 22 "hermione" 23 ); 24 const [input, setInput] = useState(""); 25 26 // Handle setting the cookie 27 const handleSet = () => { 28 setCookie(input, { path: "/" }); 29 }; 30 31 // Handle removing the cookie 32 const handleRemove = () => { 33 removeCookie(); 34 setInput(""); 35 }; 36 37 return ( 38 <Card className="relative max-w-sm w-full"> 39 <CardHeader> 40 <CardTitle>useCookie</CardTitle> 41 <CardDescription> 42 This component uses the <code>useCookie</code> hook to manage a 43 cookie. 44 </CardDescription> 45 </CardHeader> 46 <CardContent className="space-y-4"> 47 <Input 48 value={input} 49 onChange={(e) => setInput(e.target.value)} 50 placeholder="Enter cookie value" 51 /> 52 <pre className="text-sm text-muted-foreground bg-accent w-fit px-1 py-0.5 rounded-xs"> 53 {cookieValue} 54 </pre> 55 </CardContent> 56 <CardFooter className="flex gap-2"> 57 <Button onClick={handleSet} variant="secondary"> 58 Set Cookie 59 </Button> 60 <Button onClick={handleRemove} variant="destructive"> 61 Remove Cookie 62 </Button> 63 </CardFooter> 64 </Card> 65 // <div className="max-w-md mx-auto mt-10 p-6 bg-white rounded-lg shadow-md border border-gray-200"> 66 // <h2 className="text-2xl font-bold mb-4 text-gray-800">useCookie Demo</h2> 67 // <div className="mb-4"> 68 // <label className="block text-gray-700 mb-1"> 69 // Current cookie value: 70 // </label> 71 // <div className="p-2 bg-gray-100 rounded text-gray-900 min-h-[2rem]"> 72 // {cookieValue !== undefined ? ( 73 // cookieValue 74 // ) : ( 75 // <span className="italic text-gray-400">(no value)</span> 76 // )} 77 // </div> 78 // </div> 79 // <div className="mb-4"> 80 // <label htmlFor="cookie-input" className="block text-gray-700 mb-1"> 81 // Set new value: 82 // </label> 83 // <input 84 // id="cookie-input" 85 // type="text" 86 // value={input} 87 // onChange={(e) => setInput(e.target.value)} 88 // className="w-full p-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-blue-400" 89 // placeholder="Enter cookie value" 90 // /> 91 // </div> 92 // <div className="flex gap-2"> 93 // <button 94 // onClick={handleSet} 95 // className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition" 96 // > 97 // Set Cookie 98 // </button> 99 // <button 100 // onClick={handleRemove} 101 // className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600 transition" 102 // > 103 // Remove Cookie 104 // </button> 105 // </div> 106 // </div> 107 ); 108} 109

Install

npx wkkkis-hooks add use-cookie

Options

NameTypeDefaultDescription
keystring-The key to store the value in cookie
initialValuestring-The initial value to store in cookie
options?CookieOptions-The options for the hook
pathstring/The path of the cookie
expiresDatenullThe expiration date of the cookie
maxAgenumbernullThe maximum age of the cookie
domainstringnullThe domain of the cookie
securebooleanfalseWhether the cookie is secure
sameSitestringnullThe same-site attribute of the cookie

Type Safety & Serialization

  • The hook supports any serializable value (strings, objects, etc.).
  • Non-string values are automatically serialized to JSON when setting the cookie and deserialized when reading.

Cookie Removal

  • The removeCookie function deletes the cookie by setting its expiration date to the past.

Error Handling

  • If reading or writing the cookie fails, the hook logs a warning and falls back to the initial value.

SSR & Initial Value

  • On the server, the hook always returns the initial value and does not interact with cookies.
  • The initial value is only used if the cookie does not exist.

Best Practices & Caveats

  • The hook is client-side only; on the server, it does not interact with cookies.
  • Avoid storing sensitive data in cookies.
  • Changing the key will re-read and update the value from the cookie.
  • For best performance, avoid creating new initialValue or options on every render (use useMemo if needed).
  • Some cookie options (e.g., secure , sameSite ) may be required for cross-site or secure contexts.

Files in this hook

SourceDestination
files/use-cookie.tssrc/hooks/use-cookie.ts