How to set and get cookies on next js

Installation

First, you need to install the js-cookie library in your Next.js project:

npm install js-cookie

Usage

Once installed, you can use js-cookie in your Next.js components or pages.

Set Cookies

To set a cookie, you can use the set method provided by js-cookie:

import Cookies from 'js-cookie';


// Set a cookie
Cookies.set('cookieName', 'cookieValue', { expires: 7 }); // expires in 7 days

Get Cookies

To get a cookie, you can use the get method provided by js-cookie:

import Cookies from 'js-cookie';


// Get a cookie
const cookieValue = Cookies.get('cookieName');

Remove Cookies

To remove a cookie, you can use the remove method provided by js-cookie:

import Cookies from 'js-cookie';


// Remove a cookie
Cookies.remove('cookieName');

Leave a comment

Your email address will not be published. Required fields are marked *