Skip to content

Commit

Permalink
fix(Portal): state init on SSR (#1954)
Browse files Browse the repository at this point in the history
  • Loading branch information
vepor committed Jun 16, 2020
1 parent bd7f59f commit 1f43f7a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/Portal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ import ReactDOM from "react-dom";
import type { Props } from "./index";

const Portal = ({ renderInto, children }: Props) => {
const [el] = React.useState(() => document.createElement("div"));
const [node] = React.useState(() =>
renderInto && document.getElementById(renderInto)
? document.getElementById(renderInto)
: document.body,
);
const [el] = React.useState(() => {
if (typeof window !== "undefined") {
return document.createElement("div");
}
return null;
});
const [node] = React.useState(() => {
if (typeof window !== "undefined") {
return renderInto && document.getElementById(renderInto)
? document.getElementById(renderInto)
: document.body;
}
return null;
});
React.useLayoutEffect(() => {
if (node) {
if (node && el) {
node.appendChild(el);
}
return () => {
if (node) {
if (node && el) {
node.removeChild(el);
}
};
Expand Down

0 comments on commit 1f43f7a

Please sign in to comment.