Repository navigation
fix(useIntersection): accept nullable element refs - #2738
Open
wanxiankai wants to merge 1 commit into
Open
wanxiankai wants to merge 1 commit into
wanxiankai wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Description
Fixes #2612.
With React 19's types,
useRef<HTMLDivElement>(null),useRef(null), andcreateRef<HTMLDivElement>()return refs whosecurrenttype explicitly includesnull. Passing those refs touseIntersectioncurrently fails with TS2345 because its parameter is declared asRefObject<HTMLElement>.Accept
RefObject<HTMLElement | null>so the declaration matches the hook's existing null handling. The implementation continues to observe a populated ref and disconnect when the element is removed.The tests now use a typed
createRef, coveruseRef(null), and exercise an explicitly nullable ref from null to an element and back to null. The test imports the hook directly so that its types can also be checked independently against different React definitions.Type of change
Validation
yarn test --watchman=false: 76 suites and 494 tests passed.useIntersectionsuite: 11 tests passed; 100% statements, branches, functions, and lines.yarn lint:typesandyarn build: passed, including CJS and ESM declaration output.yarn lint: passed with 97 existing warnings and no errors.@types/react17.0.0, 18.3.1, and 19.0.0: the updated test file and a consumer fixture using all the ref forms above compile with all three versions. With the original signature, React 19 produces three TS2345 errors in the updated test file and four in the consumer fixture. String refs and SVG element refs remain rejected in every version.The project's dependencies are unchanged. Runtime tests use the project's React 17 installation; the React 18 and 19 checks above validate types.
Checklist
yarn test)yarn lint)yarn lint:types)Implementation and PR text were prepared with AI assistance (OpenAI Codex).