Comments (3)
Note for anyone else who runs into this:
I took a similar approach as @pimterry suggests and did something like this:
const [shouldRenderTable, setShouldRenderTable] = useState(false);
useEffect(() => {
setShouldRenderTable(true);
}, []);
const portalNode = useMemo(() => {
if (!shouldRenderTable) {
return null;
}
return portals.createHtmlPortalNode();
}, [shouldRenderTable]);
...
return (
<div>
{
portalNode && (
<portals.InPortal node={portalNode}>
<ChildToRender/>
</portals.InPortal>
)
}
...
{
portalNode && (
<portals.OutPortal node={portalNode}/>
)
}
</div>
)
Seems to work fine.
from react-reverse-portal.
@codetheweb Nice solution, thank you. For me, even slightly simpler version works (Gatsby site):
const MyComponent = () => {
const isSSR = typeof window === "undefined";
const portalNode = useMemo(() => {
if (isSSR) {
return null;
}
return portals.createHtmlPortalNode();
}, []);
return (
<div>
{portalNode && (
<portals.InPortal node={portalNode}>
<ChildToRender />
</portals.InPortal>
)}
...
{portalNode && <portals.OutPortal node={portalNode} />}
</div>
);
};
from react-reverse-portal.
Yes, that makes sense. I'm not doing SSR myself at all, so I'm unlikely to look at this any time soon, but I'd happily accept PRs.
Although it should be doable I suspect it's complicated to make everything work nicely server side, so as a very first step I'd suggest we simply avoid crashing but render nothing at all, and leave it all to be rendered on the client.
from react-reverse-portal.
Related Issues (20)
- iframes reload from source HOT 6
- Race condition when swapping nodes between <OutPortal/>s HOT 5
- Any way to prevent unmounting of the portal contents when changing portals that aren't located within the same component? HOT 2
- Child components are re-rendered when switching their OutPortal HOT 1
- How to Create Multiple InPortals HOT 1
- Not rendering before an outPortal is used HOT 2
- Unhandled Rejection (NotFoundError): Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node. HOT 9
- Giving style to intermediary div HOT 3
- How to *destroy* a portal? HOT 3
- Migrate from Travis to Github Actions? HOT 1
- Rendering portal in a new window causes error: "html" portalNodes must be used with html elements, but OutPortal is within DIV HOT 1
- Nextjs error HOT 1
- Queries around library usage HOT 1
- Doesn't work when there is nested `portals.OutPoral` HOT 6
- moving OutPortal doesn't cause unmount/mount, but *does* cause re-render HOT 2
- Props stay the same when InPortal content is hidden HOT 3
- Text field autoFocus doesn't work in a portal HOT 1
- Scroll position doesn't maintain HOT 2
- Some non-hook function results are not preserved after conditional OutPortal HOT 2
- Support for Preact HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-reverse-portal.