Skip to content

Commit

Permalink
CookieBanner: Avoid calling onInit on every re-render
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
thgreasi authored and JSReds committed Dec 14, 2023
1 parent 046318e commit c14bb01
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/CookiesBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export const CookiesBanner = ({
[cookies, onChange],
);

const [localStorageCookies, setLocalStorageCookies] = useState<
Dictionary<Cookie> | undefined
>(() => getFromLocalStorage(localStorageKey));

const handleSaveOrAcceptAll = useCallback(() => {
const newCookies: Dictionary<Cookie> = { ...cookies };

Expand All @@ -76,6 +80,7 @@ export const CookiesBanner = ({
}
setToLocalStorage(localStorageKey, newCookies);
onClose?.(newCookies);
setLocalStorageCookies(newCookies);
}, [localStorageKey, showCustomizeView, internalCookies, cookies, onClose]);

const handleReject = useCallback(() => {
Expand All @@ -86,14 +91,12 @@ export const CookiesBanner = ({
setToLocalStorage(localStorageKey, newCookies);
setInternalCookies(newCookies);
onClose?.(newCookies);
setLocalStorageCookies(newCookies);
}, [cookies, localStorageKey, onClose]);

const localStorageCookies: Dictionary<Cookie> | undefined =
getFromLocalStorage(localStorageKey);

useEffect(() => {
onInit?.(localStorageCookies);
}, [localStorageCookies, onInit]);
}, []);

if (!show || !!localStorageCookies) {
return null;
Expand Down

0 comments on commit c14bb01

Please sign in to comment.