DEV Community

Coder
Coder

Posted on • Updated on

How to set a cookie in React

If you're building a web application with React, you might need to set a cookie to store user preferences, login information, or other data that persists across sessions. Cookies are small text files that a website can store on a user's web browser. In this guide, we'll show you how to set a cookie in React and how to access its value in your application.

What is a cookie?

A cookie is a small text file that a website can store on a user's web browser. When a user visits a website, the website can set a cookie to store information about the user's visit. This information can include login information, user preferences, and other data that should persist across sessions.

Cookies can have an expiration date, which determines how long the cookie will remain on the user's browser. When the cookie expires, the browser will automatically delete it. Cookies can also have a domain and a path attribute that determines when and where the cookie can be accessed.

Setting a cookie in React

To set a cookie in React, you'll need to use the document.cookie API. This API allows you to set, retrieve, and delete cookies in the browser. Here's an example of how to set a cookie in React:

document.cookie = "myCookie=hello";
Enter fullscreen mode Exit fullscreen mode

In this example, we're setting a cookie called "myCookie" with a value of "hello". The cookie will be stored on the user's browser until it either expires or is deleted.

However, setting a cookie like this can be dangerous, as it allows for XSS attacks. Anyone with access to the browser can view and modify cookies if they are not encrypted or verified by the server. That's why you should always encrypt the cookie's data or verify it to prevent unauthorized access.

Encrypting a cookie in React

To encrypt a cookie in React, you can use a library like js-cookie or cookie. Here's an example of how to use js-cookie to set an encrypted cookie:

import Cookies from 'js-cookie';

Cookies.set('myCookie', 'hello', { secure: true });
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the js-cookie library to set a cookie called "myCookie" with a value of "hello". The { secure: true } option ensures that the cookie will only be sent over HTTPS, which is more secure than HTTP. You can also add other options, like the cookie's expiration time and domain.

By using a library like js-cookie, you can ensure that the cookie's data is encrypted and safe from unauthorized access.

Reading a cookie in React

To read a cookie in React, you can use the document.cookie API or a cookie library like js-cookie. Here's an example of how to use js-cookie to read a cookie:

import Cookies from 'js-cookie';

const cookieData = Cookies.get('myCookie');

console.log(cookieData); // "hello"
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the js-cookie library to get the value of the "myCookie" cookie. The get() method returns the value of the cookie as a string. You can then use the cookie's value in your application as needed.

Deleting a cookie in React

To delete a cookie in React, you can use the document.cookie API or a cookie library like js-cookie. Here's an example of how to use js-cookie to delete a cookie:

import Cookies from 'js-cookie';

Cookies.remove('myCookie');
Enter fullscreen mode Exit fullscreen mode

In this example, we're using the js-cookie library to remove the "myCookie" cookie from the user's browser. You can also add options like the cookie's domain and path if needed.

Best practices for using cookies in React

Here are some best practices to keep in mind when using cookies in React:

  • Always encrypt sensitive data in cookies to prevent unauthorized access.
  • Verify the cookie's data on the server-side to ensure that it hasn't been tampered with.
  • Set an expiration date or a maximum age for cookies to ensure that they don't persist indefinitely.
  • Use secure cookies that can only be sent over HTTPS to prevent man-in-the-middle attacks.
  • Avoid setting cookies with sensitive information or personal data that can identify the user.

By following these best practices, you can ensure that your cookies are secure and protect your users' privacy.

Conclusion

Setting a cookie in React is a powerful way to store user preferences, login information, and other data that persists across sessions. However, it's important to use best practices to ensure that your cookies are safe and secure. By encrypting sensitive data, verifying cookie data, and setting expiration dates, you can protect your users' privacy and prevent unauthorized access to their data. With js-cookie and other cookie libraries, setting and managing cookies in React is easier than ever.

Top comments (0)