How to enable SSR of portaled content?
al4str opened this issue ยท 9 comments
I use simple <Portal><Modal /></Portal>
to render my modals.
But can't see it rendering in view-source
tab.
Something missing in the docs?
@al4str currently it's not supported because of https://github.com/tajo/react-portal/blob/master/src/Portal.js#L15
But I have some ideas how we can make it work
Fair enough. It would be better to say: "It doesn't break SSR". Portal is expecting DOM node as a target, so it's not really possible to SSR it?
@tajo what are you thinking about this approach?
https://michalzalecki.com/render-react-portals-on-the-server/
Why not just render the children, then?
const Portal = props => {
if (!canUseDOM) return props.children;
return ReactDOM.createPortal(props.children, props.node);
}
@tajo I think it can be edge case with hash in url. For example https://domain.com/page#popup
Ah, is this why I'm seeing the error
Warning: Expected server HTML to contain a matching <div> in <header>.
when trying to render <Portal />
?
When I revert to react-portal@3
this error goes away and the portal continues to work, so I'm doing that for now. (although having to pass in isOpened
is a little clunky)