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

refactor: Change from useEffect to useSyncExternalStore #359

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

minsgy
Copy link

@minsgy minsgy commented May 30, 2024

as-is

The original code used useEffect to register the visibilitychange event on the document and update the state based on the document's visibility state in the event handler.

to-be

The updated code uses useSyncExternalStore to synchronize the external state changes. useSyncExternalStore provides a subscribe function to detect changes in the external state and a function to retrieve the current state. This allows for efficient observation of the document's visibility state changes and updating the state accordingly.
These changes have improved the readability and efficiency of the code.

Copy link

vercel bot commented May 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-hot-toast ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 21, 2024 10:26am

Copy link

vercel bot commented May 30, 2024

@minsgy is attempting to deploy a commit to the analytics-billing-vtest314 Team on Vercel.

A member of the Team first needs to authorize it.

@minsgy minsgy changed the title refactor: useSyncExternalStore useStore refactor refactor: Change from useEffect to useSyncExternalStore May 30, 2024
@timolins
Copy link
Owner

Thanks for this! I will merge this as part of the next major release, since it requires React 18+.

Required for `useSyncExternalStore`
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.

2 participants