Conversation
…or stable handler reference
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the useVisibilityEvent hook by replacing useCallback with usePreservedCallback to maintain a stable function reference for the visibility change handler, preventing unnecessary re-execution of the effect and re-attachment of event listeners.
- Replaced
useCallbackwithusePreservedCallbackfor the visibility change handler - Updated documentation to clarify the default value for
options.immediate - Removed unnecessary
useCallbackimport
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/hooks/useVisibilityEvent/useVisibilityEvent.ts | Refactored to use usePreservedCallback and updated imports |
| src/hooks/useVisibilityEvent/useVisibilityEvent.md | Added clarification about the default value for options.immediate |
| src/hooks/useVisibilityEvent/ko/useVisibilityEvent.md | Added clarification about the default value for options.immediate in Korean |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #276 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 37
Lines 1093 1091 -2
Branches 324 324
=========================================
- Hits 1093 1091 -2 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR refactors the
useVisibilityEventhook to useusePreservedCallbackfor the visibility change handler.By doing so, the
handleVisibilityChangefunction reference remains stable across component re-renders, preventinguseEffectfrom unnecessarily re-executing and re-attaching event listeners.This reduces the overhead of continuously adding and removing the
visibilitychangeevent listener, providing a performance boost.Additionally, even when the
callbackchanges,usePreservedCallbackensures the latest callback is always invoked, maintaining correctness.Added initial values to the
options.immediatedocumentation.Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?