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

useCookie does not react to changes in the passed in "cookieName" #2590

Open
ajwootto opened this issue Aug 15, 2024 · 2 comments
Open

useCookie does not react to changes in the passed in "cookieName" #2590

ajwootto opened this issue Aug 15, 2024 · 2 comments

Comments

@ajwootto
Copy link

What is the current behavior?
Since the useCookie hook works by initializing a piece of state to the value of Cookie.get with the passed in cookie name, it does not react to any changes to the cookie name that is passed in to the hook, if that value is dynamic.

Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than react-use. Paste the link to your JSFiddle or CodeSandbox example below:

For example:

const MyComponent = ({cookieName}) => {
   const cookieValue = useCookie(cookieName)
...
}

cookieValue will always be the value corresponding to the first cookieName that is passed in, and if that value changes on a subsequent re-render then the hook does not react to it changing and continues to serve the value from the first cookieName

What is the expected behavior?
The hook should react appropriately to the cookieName that is passed in and change the returned value to reflect the value of that cookie.

@matieydjato
Copy link

Hi! It looks like there might be an issue with how the useCookie hook is being used. Review the react-use documentation on useCookie to ensure it's being implemented correctly. It covers how to properly initialize and manage cookies within your components.

@ajwootto
Copy link
Author

Hi @matieydjato, I have read that documentation, the issue is that the hook assumes the cookie name being passed to it is a static string. If the name itself is dynamic and can change, the hook fails to react properly to the change in name. If that is intended behaviour then I think it would be good to explicitly call that out, since normally I'd expect a hook to respond reactively to any change in its arguments.

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

No branches or pull requests

2 participants