Skip to content

Commit a190669

Browse files
authored
fix: Portal render logic (#8)
* test: test driven * test: test driven
1 parent 0ae6d7a commit a190669

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/Portal.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ const Portal = React.forwardRef<any, PortalProps>((props, ref) => {
5858
children,
5959
} = props;
6060

61-
const [mergedRender, setMergedRender] = React.useState(open);
61+
const [shouldRender, setShouldRender] = React.useState(open);
62+
63+
const mergedRender = shouldRender || open;
6264

6365
// ====================== Should Render ======================
6466
React.useEffect(() => {
6567
if (autoDestroy || open) {
66-
setMergedRender(open);
68+
setShouldRender(open);
6769
}
6870
}, [open, autoDestroy]);
6971

tests/index.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,33 @@ describe('Portal', () => {
220220
expect(portalRef.current).toBeFalsy();
221221
});
222222
});
223+
224+
it('first render should ref accessible', () => {
225+
let checked = false;
226+
227+
const Demo = ({ open }: { open?: boolean }) => {
228+
const pRef = React.useRef();
229+
230+
React.useEffect(() => {
231+
if (open) {
232+
checked = true;
233+
expect(pRef.current).toBeTruthy();
234+
}
235+
}, [open]);
236+
237+
return (
238+
<Portal open={open}>
239+
<div>
240+
<p ref={pRef} />
241+
</div>
242+
</Portal>
243+
);
244+
};
245+
246+
const { rerender } = render(<Demo />);
247+
expect(checked).toBeFalsy();
248+
249+
rerender(<Demo open />);
250+
expect(checked).toBeTruthy();
251+
});
223252
});

0 commit comments

Comments
 (0)