refactor(useRefEffect): Improve useRefEffect hook by adding generic types in docs and strict null check in implementation #273
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 introduces two distinct improvements to the
useRefEffect
hook: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
null
check (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 fix
to format and lint the code and docs?yarn run test:coverage
to make sure there is no uncovered line?