Hello {this.state.name}!
Universal cookies for React
Integrations
universal-cookie
- Universal cookies for JavaScriptuniversal-cookie-express
- Hook cookies get/set on Express for server-rendering
Minimum requirement
react-cookie @ v3.0+
- React.js >= 16.3.0 (new context API + forward ref)
react-cookie @ v0.0-v2.2
- React.js >= 15
Getting started
npm install react-cookie
or in the browser (global variable ReactCookie
):
<CookiesProvider />
Set the user cookies
On the server, the cookies
props must be set using req.universalCookies
or new Cookie(cookieHeader)
withCookies(Component)
Give access to your cookies anywhere. Add the following props to your component:
- cookies: Cookies instance allowing you to get, set and remove cookies.
- allCookies: All your current cookies in an object.
Cookies
get(name, [options])
Get a cookie value
- name (string): cookie name
- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
getAll([options])
Get all cookies
- options (object):
- doNotParse (boolean): do not convert the cookie into an object no matter what
set(name, value, [options])
Set a cookie value
- name (string): cookie name
- value (string|object): save the value and stringify the object if needed
- options (object): Support all the cookie options from RFC 6265
- path (string): cookie path, use
/
as the path if you want your cookie to be accessible on all pages - expires (Date): absolute expiration date for the cookie
- maxAge (number): relative max age of the cookie from when the client receives it in second
- domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
- secure (boolean): Is only accessible through HTTPS?
- httpOnly (boolean): Is only the server can access the cookie?
- sameSite (boolean|lax|strict): Strict or Lax enforcement
- path (string): cookie path, use
remove(name, [options])
Remove a cookie
- name (string): cookie name
- options (object): Support all the cookie options from RFC 6265
- path (string): cookie path, use
/
as the path if you want your cookie to be accessible on all pages - expires (Date): absolute expiration date for the cookie
- maxAge (number): relative max age of the cookie from when the client receives it in second
- domain (string): domain for the cookie (sub.domain.com or .allsubdomains.com)
- secure (boolean): Is only accessible through HTTPS?
- httpOnly (boolean): Is only the server can access the cookie?
- sameSite (boolean|lax|strict): Strict or Lax enforcement
- path (string): cookie path, use
Simple Example
// Root.jsx;;; { return <CookiesProvider> <App /> </CookiesProvider> ;}
// App.jsx;;; ; static propTypes = cookies: isRequired ; { superprops; const cookies = props; thisstate = name: cookies || 'Ben' ; } { const cookies = thisprops; cookies; this; } { const name = thisstate; return <div> <NameForm name=name onChange=thishandleNameChange /> thisstatename && <h1>Hello thisstatename!</h1> </div> ; } App;
Server-Rendering Example
// src/components/App.js;;; ; static propTypes = cookies: isRequired ; { superprops; const cookies = props; thisstate = name: cookies || 'Ben' ; } { const cookies = thisprops; cookies; this; } { const name = thisstate; return <div> <NameForm name=name onChange=thishandleNameChange /> thisstatename && <h1>Hello thisstatename!</h1> </div> ; } App;
// src/server.js;;; ;; { const markup = ReactDOMServer; const html = ReactDOMServer; res;}
// src/client.js;;; ; const appEl = document; ReactDOM;
// server.js; const express = ;const serverMiddleware = default;const cookiesMiddleware = ; const app = ; app ; app;