|
| 1 | +# useCookieConsent hook for React.js |
| 2 | + |
| 3 | +[](https://github.com/use-cookie-consent/use-cookie-consent-react/actions) |
| 4 | +[](https://www.npmjs.com/package/@use-cookie-consent/react) |
| 5 | +[](https://www.npmjs.com/package/@use-cookie-consent/react) |
| 6 | +[](https://github.com/use-cookie-consent/use-cookie-consent-react/actions/workflows/codecov.yml) |
| 7 | + |
| 8 | +[](https://github.com/use-cookie-consent/use-cookie-consent-react/blob/main/LICENSE) |
| 9 | + |
| 10 | +## Description |
| 11 | + |
| 12 | +This package provides a wrapper around [`@use-cookie-consent/core`](https://github.com/use-cookie-consent/use-cookie-consent-core) package to provide best experience for React applications. Namely, it provides you React context, which provides all the functionality of [core](https://github.com/use-cookie-consent/use-cookie-consent-core) package but with reactive updates. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @use-cookie-consent/react |
| 18 | +``` |
| 19 | + |
| 20 | +or |
| 21 | + |
| 22 | +```bash |
| 23 | +yarn add @use-cookie-consent/react |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### Basic usage |
| 29 | + |
| 30 | +The first step to this setup is to wrap your app in the `CookieConsentProvider`: |
| 31 | + |
| 32 | +```jsx |
| 33 | +import { CookieConsentProvider } from '@use-cookie-consent/react' |
| 34 | + |
| 35 | +export default function App() { |
| 36 | + return { |
| 37 | + <CookieConsentProvider> |
| 38 | + ... |
| 39 | + </CookieConsentProvider> |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +Then you can read and update the cookie consent data from any component which is inside of the `<CookieConsentProvider>`. Both components you see below are not connected to each other in any way except the `useCookieConsentContext` function. You would put both of them separately somewhere in your app WITHOUT connecting them by any callbacks or passing state through props. |
| 45 | + |
| 46 | +```jsx |
| 47 | +// This component is an example of a component that you use to |
| 48 | +// accept/decline cookie consent, without reading any data. |
| 49 | +const CookieBanner = () => { |
| 50 | + const { acceptAllCookies, declineAllCookies, acceptCookies } = |
| 51 | + useCookieConsentContext(); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div> |
| 55 | + <button onClick={acceptAllCookies}>Accept all</button> |
| 56 | + <button onClick={() => acceptCookies({ thirdParty: true })}> |
| 57 | + Accept third-party |
| 58 | + </button> |
| 59 | + <button onClick={() => acceptCookies({ firstParty: true })}> |
| 60 | + Accept first-party |
| 61 | + </button> |
| 62 | + <button onClick={declineAllCookies}>Reject all</button> |
| 63 | + </div> |
| 64 | + ); |
| 65 | +}; |
| 66 | +``` |
| 67 | + |
| 68 | +```jsx |
| 69 | +// This component is an example of a component which would show |
| 70 | +// the state of the cookie consent, without updating anything. |
| 71 | +const CookiesPreview = () => { |
| 72 | + const { consent } = useCookieConsentContext(); |
| 73 | + |
| 74 | + return ( |
| 75 | + <div> |
| 76 | + <div> |
| 77 | + {`Third-party cookies ${consent.thirdParty ? 'approved' : 'rejected'}`} |
| 78 | + </div> |
| 79 | + <div> |
| 80 | + {`First-party cookies ${consent.firstParty ? 'approved' : 'rejected'}`} |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +}; |
| 85 | +``` |
| 86 | + |
| 87 | +Thanks to @pixelass for prividing the examples you see above. |
| 88 | + |
| 89 | +## Contributors |
| 90 | + |
| 91 | +- [Antoni Silvestrovic](https://github.com/bring-shrubbery) |
| 92 | +- [Gregor Adams](https://github.com/pixelass) |
| 93 | + |
| 94 | +## License |
| 95 | + |
| 96 | +[MIT](https://github.com/use-cookie-consent/use-cookie-consent-react/blob/main/LICENSE) |
0 commit comments