Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
react-use-cookie
Advanced tools
A React hook for managing cookies with no dependencies.
npm install react-use-cookie
or
yarn add react-use-cookie
useCookie
import useCookie from 'react-use-cookie';
export default (props) => {
const [userToken, setUserToken, removeUserToken] = useCookie('token', '0');
render(
<div>
<p>{userToken}</p>
<button onClick={() => setUserToken('123')}>Change token</button>
<button onClick={removeUserToken}>Remove token</button>
</div>
);
};
In this example you can also set custom cookie options by passing an options object to setUserToken
:
setUserToken('abcd', {
days: 365,
SameSite: 'Strict',
Secure: true,
});
See setCookie for more option information about this.
As a convenience this package also exports the get and set functions so they can be used directly.
getCookie
If you need to access a cookie outside of a React component, you can use the
exported getCookie
function:
import { getCookie } from 'react-use-cookie';
const getUser = () => {
const xsrfToken = getCookie('XSRF-TOKEN');
// use to call your API etc
};
setCookie
If you need to set a cookie outside of a React component, you can use the
exported setCookie
function:
import { setCookie } from 'react-use-cookie';
const saveLocale = (locale) => {
setCookie('locale', locale, {
days: 1,
domain: 'github.com',
SameSite: 'Lax',
Secure: true,
});
};
You can also specify cookie options as a third argument:
{
// The number of days the cookie is stored (defaults to 7)
days?: number;
// The path of the cookie (defaults to '/')
path?: string;
// Browser defaults unless set
domain?: string;
SameSite?: 'None' | 'Lax' | 'Strict';
Secure?: boolean;
HttpOnly?: boolean;
}
removeCookie
If you need to remove a cookie outside of a React component, you can use the
exported removeCookie
function:
import { removeCookie } from 'react-use-cookie';
removeCookie('token');
FAQs
A React hook for managing cookies with no dependencies.
The npm package react-use-cookie receives a total of 14,016 weekly downloads. As such, react-use-cookie popularity was classified as popular.
We found that react-use-cookie demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.