1
1
import CookieConsent from 'react-cookie-consent'
2
2
import { CookieConsentProps } from 'react-cookie-consent/dist/CookieConsent.props'
3
- import React , { createContext , PropsWithChildren , useContext , useMemo , useState } from 'react'
3
+ import React , { createContext , PropsWithChildren , useContext , useEffect , useState } from 'react'
4
4
import { hasUserDecidedOnConsent } from './local-storage-constants'
5
5
6
6
const CookieConsentContext = createContext ( {
7
7
visibleByDefault : false ,
8
- setVisibleByDefault ( visible : boolean ) { } ,
8
+ setVisibleByDefault ( visible : boolean ) {
9
+ }
9
10
} )
10
11
11
- export const CookieConsentContextWrapper : React . FC < PropsWithChildren < { visibleByDefault ?: boolean } > > = function ( {
12
- children,
13
- visibleByDefault,
14
- } ) {
12
+ export const CookieConsentContextWrapper : React . FC < PropsWithChildren < { visibleByDefault ?: boolean } > > = function ( {
13
+ children,
14
+ visibleByDefault
15
+ } ) {
15
16
const [ visible , setVisible ] = useState ( ( ) =>
16
17
typeof visibleByDefault == 'boolean' ? visibleByDefault : ! hasUserDecidedOnConsent ( )
17
18
)
@@ -27,7 +28,7 @@ class VisibleCookieConsent extends CookieConsent {
27
28
super ( props )
28
29
this . state = {
29
30
...this . state ,
30
- visible : true ,
31
+ visible : true
31
32
}
32
33
}
33
34
}
@@ -37,9 +38,15 @@ export function useCookieConsentContext() {
37
38
}
38
39
39
40
export const ContextualCookieConsent : React . FC < Partial < CookieConsentProps > > = props => {
41
+ const [ isClient , setIsClient ] = useState ( false )
42
+
43
+ useEffect ( ( ) => {
44
+ setIsClient ( true )
45
+ } , [ ] )
46
+
40
47
const configuration = useCookieConsentContext ( )
41
48
42
49
const Component = configuration . visibleByDefault ? VisibleCookieConsent : CookieConsent
43
50
44
- return < Component { ...props } />
51
+ return isClient && < Component { ...props } />
45
52
}
0 commit comments