Docs
useCookieStorage
Store, retrieve, and synchronize data from the browser's Cookie Store API with useCookieStorage.
Loading...
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-cookie-storage
Usage
"use client"
import { useCookieStorage } from "@/hooks/use-cookie-storage"
interface UserPreferences {
theme: "light" | "dark"
fontSize: number
}
export function Component() {
const [username, setUsername] = useCookieStorage("username", "guest", {
path: "/",
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
sameSite: "Lax",
})
const [preferences, setPreferences] = useCookieStorage<UserPreferences>(
"preferences",
{ theme: "light", fontSize: 16 },
{ path: "/", secure: true }
)
return (
<div>
<div>
<h2>Username</h2>
<input value={username} onChange={(e) => setUsername(e.target.value)} />
</div>
<div>
<h2>Preferences</h2>
<select
value={preferences.theme}
onChange={(e) =>
setPreferences((prev) => ({
...prev,
theme: e.target.value as "light" | "dark",
}))
}
>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
<input
type="number"
value={preferences.fontSize}
onChange={(e) =>
setPreferences((prev) => ({
...prev,
fontSize: Number(e.target.value),
}))
}
/>
</div>
</div>
)
}
Example Usage
Storing a Primitive Value (e.g., a counter)
export function CounterExample() {
const [counter, setCounter] = useCookieStorage("counter", 0, {
path: "/",
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // Expires in 7 days
})
return (
<div>
<p>Counter: {counter}</p>
<button onClick={() => setCounter((c) => c + 1)}>Increment</button>
</div>
)
}
Storing an Object (e.g., user preferences)
interface UserPreferences {
theme: "light" | "dark"
fontSize: number
}
export function PreferencesExample() {
const [preferences, setPreferences] = useCookieStorage<UserPreferences>(
"preferences",
{ theme: "light", fontSize: 16 }
)
return (
<div>
<label>
Theme:
<select
value={preferences.theme}
onChange={(e) =>
setPreferences((prev) => ({
...prev,
theme: e.target.value as "light" | "dark",
}))
}
>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<label>
Font Size:
<input
type="number"
value={preferences.fontSize}
onChange={(e) =>
setPreferences((prev) => ({
...prev,
fontSize: Number(e.target.value),
}))
}
/>
</label>
</div>
)
}
API Reference
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
key | string | The key used to access the cookie value. | - | No |
initialValue | T | The initial value to use if no cookie exists with the provided key. | - | No |
options | object | Configuration options for the cookie. | {} | Yes |
CookieOptions
Interface
Property | Type | Description |
---|---|---|
expires? | Date | (Optional) The expiration date of the cookie. |
path? | string | (Optional) The path where the cookie is accessible. Defaults to / . |
domain? | string | (Optional) The domain where the cookie is accessible. |
secure? | boolean | (Optional) If true , the cookie is only sent over HTTPS. |
sameSite? | "strict" | "lax" | "none" | (Optional) Controls whether the cookie is sent with cross-site requests. |
Return Values
Name | Type | Description |
---|---|---|
cookieState | T | The current value stored in the cookie. |
setCookie | (value: T | ((prev: T) => T)) => void | A function to update the cookie value. Accepts either a new value or a function that returns a new value. |