Created
February 25, 2020 12:04
-
-
Save daankauwenberg/bf0daf4d4a9a157a078ba4ec4559e3ab to your computer and use it in GitHub Desktop.
A React cookie consent using hooks and context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { CookieConsentProvider } from './CookieConsent' | |
import Page from './Page' | |
function App() { | |
return ( | |
<CookieConsentProvider> | |
<div className="App"> | |
<Page /> | |
</div> | |
</CookieConsentProvider> | |
); | |
} | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { useCookieConsentState, useCookieConsentDispatch } from './CookieConsent' | |
const Page = () => { | |
const cookieConsentState = useCookieConsentState() | |
const cookieConsentDispatch = useCookieConsentDispatch() | |
return ( | |
<article> | |
<h1>Title</h1> | |
<button onClick={() => cookieConsentDispatch({type: 'showCookiePopup '})}>Update cookie settings</button> | |
{cookieConsentState.isSet && cookieConsentState.marketing ? | |
<script> | |
{/* Marketing code here */} | |
</script> | |
: ''} | |
</article> | |
) | |
} | |
export default Page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment