Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the useRefEffect hook by adding generic type parameters to improve type safety and strengthening the null check implementation. The changes improve developer experience through better TypeScript typing while maintaining the same external behavior.
- Added generic type parameter
<E extends HTMLElement>to the hook interface for better type safety - Updated null check from loose equality (
== null) to strict equality (=== null) for more precise type narrowing - Updated documentation to reflect the new generic type interface
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/hooks/useRefEffect/useRefEffect.ts | Strengthened null check from loose to strict equality |
| src/hooks/useRefEffect/useRefEffect.md | Updated interface documentation to include generic type parameter |
| src/hooks/useRefEffect/ko/useRefEffect.md | Updated Korean documentation to match generic type interface |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| cleanupCallbackRef.current = () => {}; | ||
|
|
||
| if (element == null) { | ||
| if (element === null) { |
There was a problem hiding this comment.
The strict null check element === null will not catch undefined values, which could cause the callback to be executed with undefined. The original loose equality check == null was correct as it handles both null and undefined cases. This change could introduce bugs when the element is undefined.
| if (element === null) { | |
| if (element == null) { |
There was a problem hiding this comment.
Since instance is of type T | null as in React CallbackRef Type, I don't think we need to check for undefined. What do you think?
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #273 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 37 37
Lines 1093 1093
Branches 324 324
=========================================
Hits 1093 1093 🚀 New features to boost your workflow:
|
Overview
This PR introduces two distinct improvements to the
useRefEffecthook:Documentation and interface now include a generic type parameter, enhancing type safety and flexibility when using the hook with different HTMLElement types. This change improves developer experience by providing accurate typing information in the docs and type signatures.
The implementation has been updated to perform a strict
nullcheck (element === null) rather than a loose check. This strengthens type narrowing in TypeScript, ensuring that the callback is only called when a valid element is set, and avoids potential bugs related to falsy values.Together, these changes make the hook safer to use and better documented without changing its external behavior.
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?