Skip to content

Commit 793075e

Browse files
authored
chore: warning of ssr (#12)
1 parent 555b158 commit 793075e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Portal.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { createPortal } from 'react-dom';
33
import canUseDom from 'rc-util/lib/Dom/canUseDom';
4+
import warning from 'rc-util/lib/warning';
45
import { supportRef, useComposeRef } from 'rc-util/lib/ref';
56
import OrderContext from './Context';
67
import useDom from './useDom';
@@ -62,6 +63,14 @@ const Portal = React.forwardRef<any, PortalProps>((props, ref) => {
6263

6364
const mergedRender = shouldRender || open;
6465

66+
// ========================= Warning =========================
67+
if (process.env.NODE_ENV !== 'production') {
68+
warning(
69+
canUseDom() || !open,
70+
`Portal only work in client side. Please call 'useEffect' to show Portal instead default render in SSR.`,
71+
);
72+
}
73+
6574
// ====================== Should Render ======================
6675
React.useEffect(() => {
6776
if (autoDestroy || open) {

tests/ssr.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import Portal from '../src';
44

55
describe('SSR', () => {
66
it('No Render in SSR', () => {
7+
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
8+
79
renderToString(
810
<Portal open>
911
<div className="bamboo">Hello World</div>
1012
</Portal>,
1113
);
14+
15+
expect(errSpy).toHaveBeenCalledWith(
16+
"Warning: Portal only work in client side. Please call 'useEffect' to show Portal instead default render in SSR.",
17+
);
18+
errSpy.mockRestore();
1219
});
1320
});

0 commit comments

Comments
 (0)