Rendering components with identical children into the same parent container breaks
See original GitHub issueIf 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:
- Created 7 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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/
True, it’s literally 2 bytes after gzip. I shouldn’t agonize over these things so much, haha.