Describe the bug
In this issue, specifically in this commit, the possibility to pass null | undefined to the MutableRefObject was added.
After the library's rewrite in typescript, that issue got re-introduced and then solved in this issue/PR, in which the MutableRefObject became a Immutable RefObject.
In React 18 that was good, but with the React 19 changes for the useRef hook, the issue got re-introduced again.
In react 19, the MutableRefObject does not exists anymore, and RefObject is the unique interface for refs. Furthermore, the useRef hook must have an initial value.
This makes const ref = useRef() or const ref = useRef<HTMLDivElement>() to not work anymore. So we have to do this:
const ref = useRef<HTMLDivElement>(null); // The resultant type is RefObject<HTMLDivElement | null>
// or
const ref2 = useRef<HTMLDivElement>(undefined); // The resultant type is RefObject<HTMLDivElement | undefined>
Both of them conflict with the type of the RefObject of useResizeObserver:
To Reproduce
Steps to reproduce the behavior:
- Open a React 19 + typescript project
- Create an empty feature with a div.
- Create a
useRef constant, you will have to put null as initial value to pass it to the div
- Pass that ref object to the
useResizeObserver hook
Expected behavior
The useResizeObserver hook and similar ones, probably all the ones that need a ref, should accept RefObject<TElement | null | undefined> to work well in React 19 with typescript projects
Desktop:
- OS: Fedora Linux 44 (Workstation Edition)
- Browser: Brave Browser
- Version 1.91.175 (Chromium 149.0.7827.155)
Describe the bug
In this issue, specifically in this commit, the possibility to pass
null | undefinedto theMutableRefObjectwas added.After the library's rewrite in typescript, that issue got re-introduced and then solved in this issue/PR, in which the
MutableRefObjectbecame a ImmutableRefObject.In React 18 that was good, but with the React 19 changes for the
useRefhook, the issue got re-introduced again.In react 19, the
MutableRefObjectdoes not exists anymore, andRefObjectis the unique interface for refs. Furthermore, theuseRefhook must have an initial value.This makes
const ref = useRef()orconst ref = useRef<HTMLDivElement>()to not work anymore. So we have to do this:Both of them conflict with the type of the
RefObjectofuseResizeObserver:To Reproduce
Steps to reproduce the behavior:
useRefconstant, you will have to putnullas initial value to pass it to thedivuseResizeObserverhookExpected behavior
The
useResizeObserverhook and similar ones, probably all the ones that need aref, should acceptRefObject<TElement | null | undefined>to work well in React 19 with typescript projectsDesktop: