File tree Expand file tree Collapse file tree 2 files changed +8
-10
lines changed
packages/mui-utils/src/getReactElementRef Expand file tree Collapse file tree 2 files changed +8
-10
lines changed Original file line number Diff line number Diff line change @@ -5,15 +5,15 @@ import * as React from 'react';
5
5
describe ( 'getReactElementRef' , ( ) => {
6
6
it ( 'should return undefined when not used correctly' , ( ) => {
7
7
// @ts -expect-error
8
- expect ( getReactElementRef ( false ) ) . to . equal ( undefined ) ;
8
+ expect ( getReactElementRef ( false ) ) . to . equal ( null ) ;
9
9
// @ts -expect-error
10
- expect ( getReactElementRef ( ) ) . to . equal ( undefined ) ;
10
+ expect ( getReactElementRef ( ) ) . to . equal ( null ) ;
11
11
// @ts -expect-error
12
- expect ( getReactElementRef ( 1 ) ) . to . equal ( undefined ) ;
12
+ expect ( getReactElementRef ( 1 ) ) . to . equal ( null ) ;
13
13
14
14
const children = [ < div key = "1" /> , < div key = "2" /> ] ;
15
15
// @ts -expect-error
16
- expect ( getReactElementRef ( children ) ) . to . equal ( undefined ) ;
16
+ expect ( getReactElementRef ( children ) ) . to . equal ( null ) ;
17
17
} ) ;
18
18
19
19
it ( 'should return the ref of a React element' , ( ) => {
Original file line number Diff line number Diff line change @@ -5,16 +5,14 @@ import * as React from 'react';
5
5
* It will throw runtime error if the element is not a valid React element.
6
6
*
7
7
* @param element React.ReactElement
8
- * @returns React.Ref<any> | null | undefined
8
+ * @returns React.Ref<any> | null
9
9
*/
10
- export default function getReactElementRef (
11
- element : React . ReactElement ,
12
- ) : React . Ref < any > | null | undefined {
10
+ export default function getReactElementRef ( element : React . ReactElement ) : React . Ref < any > | null {
13
11
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions
14
12
if ( parseInt ( React . version , 10 ) >= 19 ) {
15
- return element . props ?. ref ;
13
+ return ( element ? .props as any ) ?. ref || null ;
16
14
}
17
15
// @ts -expect-error element.ref is not included in the ReactElement type
18
16
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189
19
- return element ?. ref ;
17
+ return element ?. ref || null ;
20
18
}
You can’t perform that action at this time.
0 commit comments