useCookie Deep Dive
The useCookie hook manages browser cookies reactively. It is particularly useful for data that needs to be sent to the server with every request, such as authentication tokens, language preferences, or currency settings.
Core Concepts
The hook provides a reactive interface for cookie management, returning a consistent tuple for accessing and modifying values.
import { useCookie } from '@archibald/server';
function CurrencySelector() {
const [currency, setCurrency, clearCurrency] = useCookie('app-currency');
return (
<select value={currency} onChange={(e) => setCurrency(e.target.value)}>
<option value="EUR">EUR</option>
<option value="USD">USD</option>
</select>
);
}
Reactivity & Synchronization
Unlike direct document.cookie access, useCookie is reactive across your entire application.
- Internal Subscriber Map: Archibald uses a
CookieHelperthat maintains a registry of subscribers. When you callsetorclearin one component, all other components using the same cookie name are notified and re-render automatically. - Focus Tracking: Cookies can sometimes be modified by external scripts or the browser's DevTools. To handle this,
useCookiecan optionally re-verify its value whenever the window regains focus (seeupdateOnRefocusoption), ensuring the UI stays in sync with the actual storage.
SSR & Server-Side Access
One of the primary advantages of cookies over localStorage is their visibility to the server.
- Isomorphic Access: During Server-Side Rendering (SSR),
useCookiecan retrieve values directly from the request headers. This allows you to personalize the initial HTML response based on the user's cookie-stored preferences. - Safe Hydration: The hook ensures that the value read on the server matches the value initialized on the client, avoiding common hydration flickering issues.
How it Works Step-by-Step
- Subscription: The hook registers a listener with the global
CookieHelperfor the specific cookie name. - Initial Value: It reads the cookie value from the request headers (on server) or
document.cookie(on client). - State Synchronization: It uses
useSyncExternalStoreto ensure the component re-renders whenever the cookie value is changed by any part of the application. - Modification:
- Calling
set(value, options)updates the browser cookie and notifies all other hook instances. - Calling
clear(options)removes the cookie and triggers a reactive update.
- Calling
- Focus Events: If enabled, the hook listens for
window.focusto perform a "lazy synchronization" check, updating the state if an external source modified the cookie while the tab was blurred.
Best Practice: Use useCookie for small pieces of state that are required by the server or that influence the initial page load (e.g., region, theme, session identifiers).