Gathering detailed insights and metrics for react-cookie
Gathering detailed insights and metrics for react-cookie
Gathering detailed insights and metrics for react-cookie
Gathering detailed insights and metrics for react-cookie
react-cookies
Load and save cookies with React
react-cookie-consent
A small, simple and customizable cookie consent bar for use in React applications.
react-use-cookie
A React hook for managing cookies with no dependencies.
@xylabs/react-cookie-consent
Common React library for all XY Labs projects that use React
npm install react-cookie
Typescript
Module System
Node Version
NPM Version
TypeScript (57.71%)
JavaScript (41.01%)
HTML (1.27%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
197 Stars
938 Commits
21 Forks
2 Watchers
11 Branches
9 Contributors
Updated on Jul 03, 2025
Latest Version
8.0.1
Package Id
react-cookie@8.0.1
Unpacked Size
69.63 kB
Size
16.56 kB
File Count
31
NPM Version
10.9.2
Node Version
22.14.0
Published on
Mar 18, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
6
Universal cookies for React
universal-cookie
- Universal cookies for JavaScriptuniversal-cookie-express
- Hook cookies get/set on Express for server-renderingnpm install react-cookie
or in the browser (global variable ReactCookie
):
1<script 2 crossorigin 3 src="https://unpkg.com/react@16/umd/react.production.js" 4></script> 5<script 6 crossorigin 7 src="https://unpkg.com/universal-cookie@7/umd/universalCookie.min.js" 8></script> 9<script 10 crossorigin 11 src="https://unpkg.com/react-cookie@7/umd/reactCookie.min.js" 12></script>
<CookiesProvider defaultSetOptions />
Set the user cookies
On the server, the cookies
props must be set using req.universalCookies
or new Cookie(cookieHeader)
useCookies([dependencies], [options])
Access and modify cookies using React hooks.
1const [cookies, setCookie, removeCookie] = useCookies(['cookie-name']);
React hooks are available starting from React 16.8
dependencies
(optional)Let you optionally specify a list of cookie names your component depend on or that should trigger a re-render. If unspecified, it will render on every cookie change.
options
(optional)1const [cookies, setCookie, removeCookie] = useCookies(['cookie-name'], { 2 doNotParse: true, 3});
cookies
Javascript object with all your cookies. The key is the cookie name.
setCookie(name, value, [options])
Set a cookie value
/
as the path if you want your cookie to be accessible on all pagesremoveCookie(name, [options])
Remove a cookie
/
as the path if you want your cookie to be accessible on all pagesupdateCookies()
Read back the cookies from the browser and triggers the change listeners. This should normally not be necessary because this library detects cookie changes automatically.
withCookies(Component)
Give access to your cookies anywhere. Add the following props to your component:
Your original static properties will be hoisted on the returned component. You can also access the original component by using the WrappedComponent
static property. Example:
1function MyComponent() { 2 return null; 3} 4const NewComponent = withCookies(MyComponent); 5NewComponent.WrappedComponent === MyComponent;
get(name, [options])
Get a cookie value
getAll([options])
Get all cookies
set(name, value, [options])
Set a cookie value
/
as the path if you want your cookie to be accessible on all pagesremove(name, [options])
Remove a cookie
/
as the path if you want your cookie to be accessible on all pages1// Root.tsx 2import React from 'react'; 3import App from './App'; 4import { CookiesProvider } from 'react-cookie'; 5 6export default function Root(): React.ReactElement { 7 return ( 8 <CookiesProvider defaultSetOptions={{ path: '/' }}> 9 <App /> 10 </CookiesProvider> 11 ); 12}
1// App.tsx 2import React from 'react'; 3import { useCookies } from 'react-cookie'; 4 5import NameForm from './NameForm'; 6 7interface CookieValues { 8 name?: string; 9} 10 11function App(): React.ReactElement { 12 const [cookies, setCookie] = useCookies<'name', CookieValues>(['name']); 13 14 function onChange(newName: string): void { 15 setCookie('name', newName); 16 } 17 18 return ( 19 <div> 20 <NameForm name={cookies.name || ''} onChange={onChange} /> 21 {cookies.name && <h1>Hello {cookies.name}!</h1>} 22 </div> 23 ); 24} 25 26export default App;
1// Root.tsx 2import React from 'react'; 3import App from './App'; 4import { CookiesProvider } from 'react-cookie'; 5 6export default function Root(): React.ReactElement { 7 return ( 8 <CookiesProvider> 9 <App /> 10 </CookiesProvider> 11 ); 12}
1// App.tsx 2import React, { Component } from 'react'; 3import { withCookies, Cookies, ReactCookieProps } from 'react-cookie'; 4 5import NameForm from './NameForm'; 6 7interface State { 8 name: string; 9} 10 11interface Props extends ReactCookieProps { 12 cookies: Cookies; 13} 14 15class App extends Component<Props, State> { 16 constructor(props: Props) { 17 super(props); 18 19 const { cookies } = props; 20 this.state = { 21 name: cookies.get('name') || 'Ben', 22 }; 23 } 24 25 handleNameChange(name: string): void { 26 const { cookies } = this.props; 27 28 cookies.set('name', name, { path: '/' }); 29 this.setState({ name }); 30 } 31 32 render(): React.ReactNode { 33 const { name } = this.state; 34 35 return ( 36 <div> 37 <NameForm name={name} onChange={this.handleNameChange.bind(this)} /> 38 {this.state.name && <h1>Hello {this.state.name}!</h1>} 39 </div> 40 ); 41 } 42} 43 44export default withCookies(App);
1// src/components/App.ts 2import React from 'react'; 3import { useCookies } from 'react-cookie'; 4 5import NameForm from './NameForm'; 6 7function App() { 8 const [cookies, setCookie] = useCookies(['name']); 9 10 function onChange(newName: string) { 11 setCookie('name', newName, { path: '/' }); 12 } 13 14 return ( 15 <div> 16 <NameForm name={cookies.name} onChange={onChange} /> 17 {cookies.name && <h1>Hello {cookies.name}!</h1>} 18 </div> 19 ); 20} 21 22export default App;
1// src/server.ts 2import React from 'react'; 3import ReactDOMServer from 'react-dom/server'; 4import { CookiesProvider } from 'react-cookie'; 5import { Request, Response } from 'express'; 6 7import Html from './components/Html'; 8import App from './components/App'; 9 10export default function middleware(req: Request, res: Response) { 11 const markup = ReactDOMServer.renderToString( 12 <CookiesProvider cookies={req.universalCookies}> 13 <App /> 14 </CookiesProvider>, 15 ); 16 17 const html = ReactDOMServer.renderToStaticMarkup(<Html markup={markup} />); 18 19 res.send('<!DOCTYPE html>' + html); 20}
1// src/client.ts 2import React from 'react'; 3import { createRoot } from 'react-dom/client'; 4import { CookiesProvider } from 'react-cookie'; 5 6import App from './components/App'; 7 8const root = createRoot(document.getElementById('main-app')); 9 10root.render( 11 <CookiesProvider> 12 <App /> 13 </CookiesProvider>, 14);
1// server.ts 2import express from 'express'; 3import serverMiddleware from './src/server'; 4import cookiesMiddleware from 'universal-cookie-express'; 5 6const app = express(); 7 8app.use(cookiesMiddleware()).use(serverMiddleware); 9 10app.listen(8080, function () { 11 console.log('Listening on 8080...'); 12});
No vulnerabilities found.
No security vulnerabilities found.