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

Prevent Repeat Calls of usePleaseStay #1

Open
princefishthrower opened this issue Sep 30, 2021 · 4 comments · May be fixed by #8
Open

Prevent Repeat Calls of usePleaseStay #1

princefishthrower opened this issue Sep 30, 2021 · 4 comments · May be fixed by #8
Assignees
Labels
hacktoberfest Issue designated for Hacktoberfest

Comments

@princefishthrower
Copy link
Owner

princefishthrower commented Sep 30, 2021

As the README states, one pitfall of this hook is that it should only be called once across an entire codebase - otherwise, it will create multiple timeout interactions on the document.title and will ruin the effect, not to mention add extra load to the browser. I'm looking for a code-based way to prevent this from occurring - perhaps by assigning an ID from the setInterval command:

https://developer.mozilla.org/en-US/docs/Web/API/setInterval

Logic should be something like as follows:

  • I would expect a setInterval ID to be saved in state (or I think a ref would work)
  • if the usePleaseStay() hook was called a second time and it was found this ID was already set, the developer should:
    • in development mode, see a new Error() be thrown
    • in production mode, prevent further execution of the second calling of the hook (simply return from the function)
@princefishthrower princefishthrower added the hacktoberfest Issue designated for Hacktoberfest label Sep 30, 2021
@hariharan-ingersal
Copy link

can I work on this?

@princefishthrower
Copy link
Owner Author

Definitely! I've assigned you.

hariharan-ingersal pushed a commit to hariharan-ingersal/react-use-please-stay that referenced this issue Oct 2, 2021
@hariharan-ingersal hariharan-ingersal linked a pull request Oct 2, 2021 that will close this issue
@hariharan-ingersal
Copy link

  • I would expect a setInterval ID to be saved in state (or I think a ref would work)

When the custom hook usePleaseStay is triggered from 2 different components, we can't share a common state/ref between the custom hook called from those 2 different components.

Hence tracked whether animation is triggerd or not by setting a variable on windows object in #8 . Is this fine?

@princefishthrower
Copy link
Owner Author

@hariharan-ingersal - sure. But please make the value of it the interval ID - and then we should also be sure to remove the interval on hook unmount. I'm not sure if I do that yet, I haven't looked at the hook code in detail in a while. Then for the dev / prod checking I think there are some examples in the existing code how to do that.

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

Successfully merging a pull request may close this issue.

2 participants