-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[material-ui] Element ref access React 19 compatibility #43132
Changes from 3 commits
64d96f8
eae30c4
d7e5c39
2d2c935
528f5cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Returns the ref of an element handling differences between React 19 and older versions | ||
* | ||
* @param element React.ReactElement<any> | ||
* @returns React.Ref<any> | null | ||
*/ | ||
export default function getElementRef(element: React.ReactElement<any>): React.Ref<any> | null { | ||
if (!element || !React.isValidElement(element)) { | ||
return null; | ||
} | ||
|
||
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions | ||
return (element.props as any).propertyIsEnumerable('ref') | ||
? (element.props as any).ref | ||
: // @ts-expect-error element.ref is not included in the ReactElement type | ||
// We cannot check for it, but isValidElement is true at this point | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Here are other occurrences, probably copy-pasted: https://github.com/search?q=repo%3Amui%2Fmaterial-ui%20%22TODO%20upstream%20fix%22&type=code I don't think they'll fix it now that they moved the ref to the props. I don't know if there's a better way to handle this, I couldn't find one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just recommend if you find an open issue about this, to link in the comment above so we know once it's merged that we can remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find any issue in the original PR, DefinitelyTyped issues, or DefinitelyTyped discussions. I don't know if they have discussed it previously. I don't think it will get much traction as it's probably been like this forever and it's now removed in React 19. I created a discussion asking for this and linked it. |
||
element.ref; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './getElementRef'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can likely simplify, as the
getElementRef
handles this already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored to handle this case and renamed the util accordingly 😊