Skip to content

Commit

Permalink
fix: Portal render logic (#8)
Browse files Browse the repository at this point in the history
* test: test driven

* test: test driven
  • Loading branch information
zombieJ authored Nov 4, 2022
1 parent 0ae6d7a commit a190669
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ const Portal = React.forwardRef<any, PortalProps>((props, ref) => {
children,
} = props;

const [mergedRender, setMergedRender] = React.useState(open);
const [shouldRender, setShouldRender] = React.useState(open);

const mergedRender = shouldRender || open;

// ====================== Should Render ======================
React.useEffect(() => {
if (autoDestroy || open) {
setMergedRender(open);
setShouldRender(open);
}
}, [open, autoDestroy]);

Expand Down
29 changes: 29 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,33 @@ describe('Portal', () => {
expect(portalRef.current).toBeFalsy();
});
});

it('first render should ref accessible', () => {
let checked = false;

const Demo = ({ open }: { open?: boolean }) => {
const pRef = React.useRef();

React.useEffect(() => {
if (open) {
checked = true;
expect(pRef.current).toBeTruthy();
}
}, [open]);

return (
<Portal open={open}>
<div>
<p ref={pRef} />
</div>
</Portal>
);
};

const { rerender } = render(<Demo />);
expect(checked).toBeFalsy();

rerender(<Demo open />);
expect(checked).toBeTruthy();
});
});

0 comments on commit a190669

Please sign in to comment.