Skip to content

Commit

Permalink
fix: SSR should not create div element
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Sep 28, 2022
1 parent a2ad5ed commit bbc8da7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['<rootDir>/tests/setupAfterEnv.ts']
};
setupFilesAfterEnv: ['<rootDir>/tests/setupAfterEnv.ts'],
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
"start": "dumi dev",
"test": "umi-test",
"test:coverage": "umi-test --coverage",
"test": "npm run test:client && npm run test:server",
"test:client": "umi-test --testPathIgnorePatterns=ssr.test.tsx --testPathIgnorePatterns=ssr.test.tsx",
"test:coverage": "npm run test:client --coverage",
"test:server": "umi-test --env=node tests/ssr.test.tsx",
"watch": "father dev"
},
"dependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/useDom.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import canUseDom from 'rc-util/lib/Dom/canUseDom';
import OrderContext from './Context';
import type { QueueCreate } from './Context';

Expand All @@ -14,6 +15,10 @@ export default function useDom(
debug?: string,
): [HTMLDivElement, QueueCreate] {
const [ele] = React.useState(() => {
if (!canUseDom()) {
return null;
}

const defaultEle = document.createElement('div');

if (process.env.NODE_ENV !== 'production' && debug) {
Expand Down
10 changes: 2 additions & 8 deletions tests/ssr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import { renderToString } from 'react-dom/server';
import Portal from '../src';

jest.mock('rc-util/lib/Dom/canUseDom', () => () => false);

describe('SSR', () => {
it('No Render in SSR', () => {
const { unmount } = render(
renderToString(
<Portal open>
<div className="bamboo">Hello World</div>
</Portal>,
);

expect(document.querySelector('.bamboo')).toBeFalsy();

unmount();
});
});

0 comments on commit bbc8da7

Please sign in to comment.