question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using createPortal and Router causes route to be removed

See original GitHub issue

Not too sure if this belongs more here or on preact-router. But I was investigating a strange behaviour in my app that I’m slowly moving from 8.4.2 to X.

I made a codesandbox that illustrates the issue.

It seems as that when you call createPortal in a router path, it renders the contents of the portal, but removes everything else.

I would expect it to render into the defined element, either at the beginning or the end, but it seems to replace everything.

If this should be moved to preact-router I can move it there.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JoviDeCroockcommented, May 8, 2019

This is a bug, when using createPortal it overrides the full given node instead of inserting before already existing elements.

I have tested it with a minimal example and it is.

Most minimal example to test with:

const App = () => (
  <div>
    <p>hello</p>
    {createPortal(<div>Portalled</div>, document.getElementById("root"))}
  </div>
);

render(<App />, document.getElementById("root"));

This will effectively remove <p>Hello</p> from root

0reactions
pmkroekercommented, May 8, 2019

I’ll reopen this until there is as definite answer, document.body works but document.querySelector(#root) does not when used with preact-router

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using createPortal and Router causes route to be removed
It seems as that when you call createPortal in a router path, it renders the contents of the portal, but removes everything else....
Read more >
React-Router Redirect from a portal not redirecting
Since StreamDelete is directly rendered by a Route component <Route path="/streams/:id/delete" exact component={StreamDelete} />. Solution.
Read more >
Portals - React
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. ReactDOM.createPortal(child, ......
Read more >
Understanding React Portals and Its Use-Cases
React Portal is a first-class way to render child components into a DOM node outside of the parent DOM hierarchy defined by the...
Read more >
Portals with Context - React Training
Normally we might expect the modal to be rendered as a sibling to the "Remove User" button since that's how it looks in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found