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.

Rendering components with identical children into the same parent container breaks

See original GitHub issue

If you try to render two different components into the same parent container multiple times with shared child components the browser will throw an error:

Uncaught HierarchyRequestError: Failed to execute 'replaceChild' on 'Node': The new child element contains the parent.

"preact": "4.6.2",
"preact-render-to-string": "2.4.0"

Chrome Version 50.0.2661.75 (64-bit)

This is easier to explain with some code:

class Intro extends Component {
  render(props) {
    return (<span>Hello</span>)
  }
}
class Index extends Component {
  render(props) {
    return (
      <div>
        <Intro />
      </div>)
  }
}
class Join extends Component {
  render(props) {
    return (<Intro />)
  }
}
  let components = [Index, Join, Index, Join, Index]
  // This loop simulates the user navigating forward and backwards over two components.
  components.forEach(function(Cmp) {
    render(
      <Cmp data={window.preactProps} />,
      document.body,
      document.body.firstChild)
  })

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, Apr 16, 2016

I’m going to close this issue out for now since we’ve got a solution, though I’ll likely re-reference it when I address making render() automatically detect and attach to existing rendered components.

edit: for reference, reproduction I was using to test: http://jsfiddle.net/developit/t8p15pps/

1reaction
developitcommented, Apr 16, 2016

True, it’s literally 2 bytes after gzip. I shouldn’t agonize over these things so much, haha.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional rendering tactics for React components
A simple exploration and comparison of two tactics for handling conditionally rendering components in React.
Read more >
React: different ways to enhance child components - Sho Mukai
Use React API cloneElement() ... This method can copy the child and add new props into it. ... The props.children in React parent...
Read more >
Is componentDidMount of parent called after all ...
I am facing the same problem. Async Data Loading breaks my componentDidMount() cycle because a placeholder is showed first. I guess there is...
Read more >
The mystery of React Element, children, parents and re-renders
Looking into what is React Element, exploring various children vs parents relationship in React, and how they affect re-renders.
Read more >
React.memo() vs. useMemo() - Bits and Pieces
However, as you can see, the child component's render method is also invoked whenever the parent component is re-rendered. This will trigger the...
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