React Cookie Management Library

8.1.0 · active · verified Sun Apr 19

react-cookie is a utility library for React applications that provides a universal (isomorphic) solution for managing browser cookies, enabling consistent cookie handling across both client-side and server-side rendering (SSR) environments. Its current stable version is 8.1.0, with frequent updates addressing dependencies and minor improvements, typically on a monthly cadence for patch releases and less frequently for minor/major versions as features or breaking changes are introduced. Key differentiators include its integration with the underlying `universal-cookie` library for core logic, offering React-specific hooks (`useCookies`) and a provider pattern (`CookiesProvider`) for easy consumption within React component trees. It simplifies reading, setting, and removing cookies by abstracting away the browser's `document.cookie` API and Node.js server request headers, making it particularly useful for applications requiring robust cookie management in an SSR context.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up `CookiesProvider` and using the `useCookies` hook to interact with a cookie named 'my-cookie-name'.

import React from 'react';
import { CookiesProvider, useCookies } from 'react-cookie';

function MyComponent() {
  const [cookies, setCookie, removeCookie] = useCookies(['my-cookie-name']);

  const handleSetCookie = () => {
    setCookie('my-cookie-name', 'hello-world', { path: '/', maxAge: 3600 });
  };

  const handleRemoveCookie = () => {
    removeCookie('my-cookie-name', { path: '/' });
  };

  return (
    <div>
      <h1>Cookie Value: {cookies['my-cookie-name'] || 'None Set'}</h1>
      <button onClick={handleSetCookie}>Set Cookie</button>
      <button onClick={handleRemoveCookie}>Remove Cookie</button>
      <p>This component demonstrates how to use `useCookies` to read, set, and remove cookies. The `CookiesProvider` makes the cookie context available throughout your app.</p>
    </div>
  );
}

export default function App() {
  return (
    <CookiesProvider>
      <MyComponent />
    </CookiesProvider>
  );
}

view raw JSON →