BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/guarahooks/use-cookie

UseCookie

guarahooks/use-cookie
FreeHook

Synchronizes a value with cookies.

Install it

npx shadcn@latest add https://guarahooks.com/r/use-cookie.json

Source

registry/hooks/use-cookie.tsxguarahooks.com/r/use-cookie.json
1import { useCallback, useMemo } from 'react';
2
3export type CookieOptions = {
4 path?: string;
5 expires?: Date | string;
6 maxAge?: number;
7 domain?: string;
8 secure?: boolean;
9 sameSite?: 'strict' | 'lax' | 'none';
10};
11
12/**
13 * Type-safe React hook for managing cookies.
14 * Provides methods to get, set, and remove cookies.
15 *
16 * @param key The cookie key
17 * @param initialValue Optional initial value for the cookie
18 * @returns [value, setCookie, removeCookie]
19 */
20export function useCookie<T = string>(
21 key: string,
22 initialValue?: T,
23): [T | undefined, (value: T, options?: CookieOptions) => void, () => void] {
24 // Helper to check if running in browser
25 const isBrowser = typeof document !== 'undefined';
26
27 // Parse cookie string to object
28 const getCookies = useCallback((): Record<string, string> => {
29 if (!isBrowser) return {};
30 return document.cookie.split('; ').reduce(
31 (acc, cookie) => {
32 const [k, ...v] = cookie.split('=');
33 acc[decodeURIComponent(k)] = decodeURIComponent(v.join('='));
34 return acc;
35 },
36 {} as Record<string, string>,
37 );
38 }, [isBrowser]);
39
40 // Set cookie
41 const setCookie = useCallback(
42 (val: T | undefined, options: CookieOptions = {}) => {
43 if (!isBrowser) return;
44 let encodedValue: string;
45 if (typeof val === 'string') {
46 encodedValue = encodeURIComponent(val);
47 } else {
48 encodedValue = encodeURIComponent(JSON.stringify(val));
49 }
50 let cookieStr = `${encodeURIComponent(key)}=${encodedValue}`;
51 if (options.path) cookieStr += `; path=${options.path}`;
52 if (options.expires)
53 cookieStr += `; expires=${options.expires instanceof Date ? options.expires.toUTCString() : options.expires}`;
54 if (options.maxAge) cookieStr += `; max-age=${options.maxAge}`;
55 if (options.domain) cookieStr += `; domain=${options.domain}`;
56 if (options.secure) cookieStr += '; secure';
57 if (options.sameSite) cookieStr += `; samesite=${options.sameSite}`;
58 document.cookie = cookieStr;
59 },
60 [isBrowser, key],
61 );
62
63 // Remove cookie
64 const removeCookie = useCallback(() => {
65 setCookie(undefined, { path: '/', expires: new Date(0) });
66 }, [setCookie]);
67
68 // Get cookie value (and create if missing)
69 const value = useMemo(() => {
70 const cookies = getCookies();
71 if (!(key in cookies)) {
72 if (initialValue !== undefined) {
73 setCookie(initialValue);
74 return initialValue;
75 } else {
76// … truncated

Files it writes

  • registry/hooks/use-cookie.tsx

Facts

Registry
guarahooks
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
In the index
at or before 2026-08-08
Last confirmed
yesterday

Go to the source

guarahooks.com h3rmel/guarahooks on GitHub Raw registry item This item as JSON

More from guarahooks

All 100 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
UseAbortControlleruse-abort-controllerguarahooksHooksFreeno deps
UseArrayStateuse-array-stateguarahooksHooksFreeno deps
UseAxiosuse-axiosguarahooksHooksFreeno deps
UseBatteryStatususe-battery-statusguarahooksHooksFreeno deps
UseBetterAuthuse-better-authguarahooksHooksFreeno deps
UseClickOutsideuse-click-outsideguarahooksHooksFreeno deps
UseConfirmuse-confirmguarahooksHooksFreeno deps
UseCopyToClipboarduse-copy-to-clipboardguarahooksHooksFreeno deps

Also from Kynth Studios

Built for the same person as BlockDex

SkillWorks

Claude Code skills and plugins, scored

skillworks.kynth.studio

RuleStack

AGENTS.md gallery and agent-config comparison

rulestack.kynth.studio

ToolDrift

What the AI coding tools changed last night

tooldrift.kynth.studio

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex