Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example in README.md #18

Merged
merged 1 commit into from
Jan 31, 2022
Merged

Conversation

edgarlr
Copy link
Contributor

@edgarlr edgarlr commented Jan 31, 2022

Why?

While using the package I followed the example in the README but noticed that was not setting the actual cookie in the browser. After digging I noticed that was because it needs the necessary cookies to be true to set the cookie.

https://github.com/use-cookie-consent/use-cookie-consent-core/blob/feeb78cd823b21cb9f626e84526ffcec10b8ef28/src/useCookieConsent.ts#L24-L29

And even when the default value of the necessary cookies is true, the initial value does not persist using acceptCookies(). The easiest fix was just setting the necessary cookies to true along with the new value.

I also added a basic example of the hook with custom cookie attributes.

## Why?
While using the package I followed the example in the README but noticed that was not setting the actual cookie in the browser. After digging I noticed that was because it [needs the necessary cookies to be true](https://github.com/use-cookie-consent/use-cookie-consent-core/blob/feeb78cd823b21cb9f626e84526ffcec10b8ef28/src/useCookieConsent.ts#L24) to set the cookie and even when the default value of the necessary cookies is `true`, the initial value does not persist using `acceptCookies()`. The easiest fix was just setting the necessary cookies to `true` along with the new value.

I also added a basic example of the hook with custom cookie attributes.
@edgarlr
Copy link
Contributor Author

edgarlr commented Jan 31, 2022

Another possible solution could be to update the acceptCookies() to persist initial state or always set necessary cookies to true on that method
https://github.com/use-cookie-consent/use-cookie-consent-core/blob/feeb78cd823b21cb9f626e84526ffcec10b8ef28/src/useCookieConsent.ts#L33-L35

e.g. persist current state

 const acceptCookies = (newConsent: CookieConsent) => {
    setConsent((currentConsent) => {
        ...currentConsent
        ...newConsent
     });
  };

e.g. aways set necessary cookies to true

 const acceptCookies = (newConsent: CookieConsent) => {
    setConsent({
        ...newConsent,
        necessary: true,
     });
  };

But that may cause some side effects

@codecov-commenter
Copy link

Codecov Report

Merging #18 (399f7b6) into main (feeb78c) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #18   +/-   ##
=======================================
  Coverage   93.47%   93.47%           
=======================================
  Files           3        3           
  Lines          46       46           
  Branches       15       15           
=======================================
  Hits           43       43           
  Misses          2        2           
  Partials        1        1           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update feeb78c...399f7b6. Read the comment docs.

@bring-shrubbery
Copy link
Owner

Hey @edgarlr, thanks for the PR!

Nice catch with the necessary cookies, these should definitely always be true. I added an issue for this so that I don't forget to fix it - or you could if you want to 😉

Just a thought - I think it's better to not change that cookie, instead of setting it to true every time.

@bring-shrubbery bring-shrubbery merged commit 0188f3f into bring-shrubbery:main Jan 31, 2022
@edgarlr
Copy link
Contributor Author

edgarlr commented Jan 31, 2022

@bring-shrubbery great! and sure, I can take that issue! I'll work on it and I'll open a new PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants