Skip to content

Commit bbc8da7

Browse files
committed
fix: SSR should not create div element
1 parent a2ad5ed commit bbc8da7

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
setupFilesAfterEnv: ['<rootDir>/tests/setupAfterEnv.ts']
3-
};
2+
setupFilesAfterEnv: ['<rootDir>/tests/setupAfterEnv.ts'],
3+
};

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
3737
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
3838
"start": "dumi dev",
39-
"test": "umi-test",
40-
"test:coverage": "umi-test --coverage",
39+
"test": "npm run test:client && npm run test:server",
40+
"test:client": "umi-test --testPathIgnorePatterns=ssr.test.tsx --testPathIgnorePatterns=ssr.test.tsx",
41+
"test:coverage": "npm run test:client --coverage",
42+
"test:server": "umi-test --env=node tests/ssr.test.tsx",
4143
"watch": "father dev"
4244
},
4345
"dependencies": {

src/useDom.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
3+
import canUseDom from 'rc-util/lib/Dom/canUseDom';
34
import OrderContext from './Context';
45
import type { QueueCreate } from './Context';
56

@@ -14,6 +15,10 @@ export default function useDom(
1415
debug?: string,
1516
): [HTMLDivElement, QueueCreate] {
1617
const [ele] = React.useState(() => {
18+
if (!canUseDom()) {
19+
return null;
20+
}
21+
1722
const defaultEle = document.createElement('div');
1823

1924
if (process.env.NODE_ENV !== 'production' && debug) {

tests/ssr.test.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import React from 'react';
2-
import { render } from '@testing-library/react';
2+
import { renderToString } from 'react-dom/server';
33
import Portal from '../src';
44

5-
jest.mock('rc-util/lib/Dom/canUseDom', () => () => false);
6-
75
describe('SSR', () => {
86
it('No Render in SSR', () => {
9-
const { unmount } = render(
7+
renderToString(
108
<Portal open>
119
<div className="bamboo">Hello World</div>
1210
</Portal>,
1311
);
14-
15-
expect(document.querySelector('.bamboo')).toBeFalsy();
16-
17-
unmount();
1812
});
1913
});

0 commit comments

Comments
 (0)