diff --git a/src/Dialog/Content/index.tsx b/src/Dialog/Content/index.tsx index 665de5bf..f3313d53 100644 --- a/src/Dialog/Content/index.tsx +++ b/src/Dialog/Content/index.tsx @@ -52,6 +52,10 @@ const Content = React.forwardRef((props, ref) => { } function onPrepare() { + if (!dialogRef.current?.nativeElement) { + return; + } + const elementOffset = offset(dialogRef.current.nativeElement); setTransformOrigin( diff --git a/tests/index.spec.tsx b/tests/index.spec.tsx index a00c5c4a..9ab2d51b 100644 --- a/tests/index.spec.tsx +++ b/tests/index.spec.tsx @@ -278,6 +278,18 @@ describe('dialog', () => { document.querySelector('.rc-dialog').style['transform-origin'], ).toBeTruthy(); }); + + it('should not throw error when nativeElement is not available', () => { + expect(() => { + global.onAppearPrepare?.(); + }).not.toThrow(); + + render(); + + expect(() => { + global.onAppearPrepare?.(); + }).not.toThrow(); + }); }); it('can get dom element before dialog first show when forceRender is set true ', () => {