![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
@tinkoff/browser-cookies
Advanced tools
Tiny cookies library for the browser and server environments
Tiny cookies library for the browser
Fork of browser-cookies
Using npm
npm install @tinkoff/browser-cookies
Using yarn
npm install @tinkoff/browser-cookies
import { Cookies } from '@tinkoff/browser-cookies';
const cookies = new Cookies({ sameSite: 'lax' });
cookies.set('firstName', 'Lisa');
cookies.set('firstName', 'Lisa', { expires: 365 }); // Expires after 1 year
cookies.set('firstName', 'Lisa', { secure: true, domain: 'www.example.org' });
cookies.get('firstName'); // Returns cookie value (or null)
cookies.erase('firstName'); // Removes cookie
Cookies
API:
defaults
)name
, value
[, options
])name
)name
, [, options
])Cookies.set(name
, value
[, options
])
Method to save a cookie.
argument | type | description |
---|---|---|
name | string | The name of the cookie to save. |
value | string | The value to save, [percent encoding][ref-percent-encoding] will automatically be applied. Note that only strings are allowed as value, the examples section shows how to save JSON data. |
options | object | May contain any of the properties specified in options below. If an option is not specified, the value configured in Cookies.constructor will be used. |
Cookies.get(name
)
Method that returns a cookie value, or null if the cookie is not found. [Percent encoded][ref-percent-encoding] values will automatically be decoded.
argument | type | description |
---|---|---|
name | string | The name of the cookie to retrieve. |
Cookies.erase(name
[, options
])
Method to remove a cookie.
argument | type | description |
---|---|---|
name | string | The name of the cookie to remove. |
options | object | May contain the domain and path properties specified in options below. If an option is not specified, the value configured in Cookies.constructor will be used. |
Cookies.all()
Method to get all cookies.
Returns an object containing all cookie values with the cookie names used as keys. Percent encoded names and values will automatically be decoded.
Cookies.constructor(defaults
)
defaults
argument may be used to change the default value of each option specified in options below.
The options shown in the table below may be set to instance of Cookies.constructor or passed as function argument to Cookies.set() and Cookies.erase(). Also check out the Examples further below.
Name | Type | Default | Description |
---|---|---|---|
expires | Number , Date , String | 0 | Configure when the cookie expires by using one of the following types as value:
|
domain | String | "" | The [domain][ref-cookie-domain] from where the cookie is readable.
|
path | String | "/" | The path from where the cookie is readable.
|
secure | Boolean | false | If true the cookie will only be transmitted over secure protocols like https. |
httponly | Boolean | false | If true the cookie may only be read by the web server.
|
samesite | String | "" | The samesite argument may be used to [prevent cookies from being sent along with cross-site requests][ref-samesite].
|
Count the number of a visits to a page:
import { Cookies } from '@tinkoff/browser-cookies';
const cookies = new Cookies();
// Get cookie value
const visits = cookies.get('count') || 0;
console.log("You've been here " + parseInt(visits) + " times before!");
// Increment the counter and set (or update) the cookie
cookies.set('count', parseInt(visits) + 1, {expires: 365});
JSON may be saved by converting the JSON object into a string:
import { Cookies } from '@tinkoff/browser-cookies';
const cookies = new Cookies();
// Store JSON data
const user = { firstName: 'Sofia', lastName: 'Dueñas' };
cookies.set('user', JSON.stringify(user))
// Retrieve JSON data
const userString = cookies.get('user');
alert('Hi ' + JSON.parse(userString).firstName);
The default cookie options may be changed:
import { Cookies } from '@tinkoff/browser-cookies';
// Apply defaults
const cookies = new Cookies({
secure: true,
expires: 7,
});
// 'secure' option enabled and cookie expires in 7 days
cookies.set('FirstName', 'John')
// 'secure' option enabled and cookie expires in 30 days
cookies.set('LastName', 'Smith', { expires: 30 })
The cookies.all
method can be used for more advanced functionality, for example to erase all cookies except one:
import { Cookies } from '@tinkoff/browser-cookies';
const cookies = new Cookies();
const cookieToKeep = 'FirstName'; // Name of the cookie to keep
// Get all cookies as an object
const allCookies = cookies.all();
// Iterate over all cookie names
for (let cookieName in allCookies) {
// Erase the cookie (except if it's the cookie that needs to be kept)
if (allCookies.hasOwnProperty(cookieName) && cookieName != cookieToKeep) {
cookies.erase(cookieName);
}
}
FAQs
Tiny cookies library for the browser and server environments
We found that @tinkoff/browser-cookies demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.