Skip to content

Fix useBoxMetrics not accepting ref objects with an initial null value#945

Merged
sindresorhus merged 2 commits into
vadimdemedes:masterfrom
gaac510:master
May 12, 2026
Merged

Fix useBoxMetrics not accepting ref objects with an initial null value#945
sindresorhus merged 2 commits into
vadimdemedes:masterfrom
gaac510:master

Conversation

@gaac510

@gaac510 gaac510 commented May 11, 2026

Copy link
Copy Markdown
Contributor

This pull request hopes to change the current behaviours below to the expected behaviours.

Current Behaviours

With complierOptions.strictNullChecks: true set in tsconfig.json, compilation with the below code in the project always fails due to useBoxMetrics only accepting RefObject<DOMElement> and not also RefObject<DOMElement | null>.

const ref = useRef<DOMElement>(null);
const {...} = useBoxMetrics(ref);

Further, VSCode highlights a type error regardless of the compiler options sets:
Image

Expected Behaviours

  1. When complierOptions.strictNullChecks: true is set in tsconfig.json, the code above shouldn't cause compilation to fail.
  2. useBoxMetrics should be able to accept RefObject<DOMElement | null> since it's common for a ref object (especially one that will be passed to a DOM node's ref attribute) to be created with an initial null value.
  3. VSCode shouldn't report a type error.

@sindresorhus

Copy link
Copy Markdown
Collaborator

The docs still lists the ref type as React.RefObject<DOMElement>. Since this PR changes the accepted public type to React.RefObject<DOMElement | null>, and the example right below uses useRef(null), the documented type should be updated too.

@gaac510

gaac510 commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

@sindresorhus Thanks. Updated the docs as well now.
BTW I can see my fork is now 1 commit behind but the merge request check still says "No conflicts with base branch", so... hopefully that mean I shouldn't need to sync my fork?

@gaac510 gaac510 changed the title Fix useBoxMetric not accepting ref objects with an initial null value Fix useBoxMetrics not accepting ref objects with an initial null value May 11, 2026
@gaac510

gaac510 commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

Hi, sorry, I haven't contributed to open source projects much, so I'm not familiar with the process. Is there anything else I need to do to complete this merge request?

@sindresorhus sindresorhus merged commit 7c2267c into vadimdemedes:master May 12, 2026
2 checks passed
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