cookieconsent: Bug: plugin is executed twice inside the `useEffect` hook in React
Version 3.0.0-rc.5
What is the correct way to use the API?
The way I do it is import it like shown in the docs:
import * as CookieConsent from 'vanilla-cookieconsent';
Then I try to call either CookieConsent.acceptCategory('analytics') or CookieConsent.acceptService('ga', 'analytics') on button click. The cookie gets added, its timestamp is updated, but “categories” and “services” arrays are empty.
Setting categories and services via the modal works fine.
Also, when accessing imported CookieConsent, typescript complains: “Cannot use namespace ‘CookieConsent’ as a value.” I have to cast it as CookieConsent.CookieConsentAPI for typescript to stop complaining.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (4 by maintainers)
Commits related to this issue
- Fix issue with the `useEffect` hook in React (ref. #383) — committed to orestbida/cookieconsent by orestbida 2 years ago
Not really. React 18 made things just a little more “complicated”, especially for iife + client only plugins such cookieconsent v2.
@itsam, this is the easiest way to use v2 in next.js (call api via the window object): https://stackblitz.com/edit/nextjs-euxk9k?file=components%2FCookieConsentComponent.js
I’ll update the
FAQ -> How to use in Reactsection for future users.I use the API this way with Next.js:
Then in my page component:
Then in any child component I can do the following:
You are, right, in the proposed solution, everything works as expected in stable release 2.8.6. I think the tricky part (besides limiting to load just once in the useEffect) was to use
window(e.g.window.CookieConsentApi.showSettings(0)) all the times instead of the object (e.g. cc) which either created once in the context, zustand or wherever.Thanks a lot for clarifying and giving a functional example, I am pretty sure it will be helpful for others trying to integrate with next.js
Released v3.0.0-rc.6, feel free to re-open this issue if you still encounter the problem.
@Polynook
that is indeed the proper way to use the plugin.
I believe this issue is caused by React’s new
useEffectbehaviour; theuseEffecthook is executed twice in dev/strict mode and messes up the plugin.As for the types issue, it has already been fixed in the v3 branch https://github.com/orestbida/cookieconsent/commit/93ccc795406f9c42528d22c8a17da890d2857f30.
Note: you could also use named imports