From 529571a187af3e322f153ce8dcc8ff884eb36de7 Mon Sep 17 00:00:00 2001 From: Sneh1999 Date: Fri, 5 Jan 2024 16:52:19 -0500 Subject: [PATCH] convert cookieValues to a state variable --- packages/cookie-manager/src/CookieContext.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/cookie-manager/src/CookieContext.tsx b/packages/cookie-manager/src/CookieContext.tsx index c42f808..f65a454 100644 --- a/packages/cookie-manager/src/CookieContext.tsx +++ b/packages/cookie-manager/src/CookieContext.tsx @@ -1,5 +1,5 @@ import Cookies, { CookieAttributes } from 'js-cookie'; -import React, { createContext, useCallback, useContext, useEffect } from 'react'; +import React, { createContext, useCallback, useContext, useEffect, useState } from 'react'; import { ADVERTISING_SHARING_ALLOWED, @@ -39,7 +39,7 @@ export const CookieProvider = ({ children }: Props) => { const { config, region, shadowMode, log, onPreferenceChange } = useTrackingManager(); const POLL_INTERVAL = 500; - let cookieValues: Record = getAllCookies(); + const [cookieValues, setCookieValues] = useState(() => getAllCookies()); let trackingPreference: TrackingPreference; let adTrackingPreference: AdTrackingPreference; @@ -63,12 +63,12 @@ export const CookieProvider = ({ children }: Props) => { const checkCookies = () => { const currentCookie = getAllCookies(); if (!areRecordsEqual(cookieValues, currentCookie)) { - cookieValues = currentCookie; - trackingPreference = getTrackingPreference(cookieValues, region, config); - adTrackingPreference = getAdTrackingPreference(cookieValues); + setCookieValues(currentCookie); + trackingPreference = getTrackingPreference(currentCookie, region, config); + adTrackingPreference = getAdTrackingPreference(currentCookie); setGTMVariables(trackingPreference, adTrackingPreference); const cookiesToRemove: Array = []; - Object.keys(cookieValues).forEach((c) => { + Object.keys(currentCookie).forEach((c) => { const trackerInfo = getTrackerInfo(c, config); if (REQUIRED_COOKIE_MANAGER_COOKIES.includes(c)) { return;