Rendered view is appended to the `root` element instead of replacing its contents
See original GitHub issueI noticed that when a hyperapp app initializes, the rendered view is appended to the root
element, leaving any existing content in place. I kind of expected it to replace the existing content. Doing that would allow stuff like showing the user an informative message if the app code fails to run:
<!-- index.html -->
<div id="root">
Please ensure you've enabled JavaScript and your browser is up to date.
</div>
// main.js
app({
root: document.getElementById('root'),
/* ... */
});
Any chance the rendering behavior can be changed so the view completely replaces the content of the root
element?
EDIT: Currently, I’m working around this by doing
events: {
load: function() {
document.getElementById('root').innerHTML = ''
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:18 (16 by maintainers)
Top Results From Across the Web
`render` appends instead of replacing · Issue #24 · preactjs ...
Instead, render() accepts an optional third argument that is the root element to replace. So, to emulate react's behavior just do:.
Read more >React.render replace container instead of inserting into
It replaces the contents of whatever element you render into. The best you can do for now is render the React component separately...
Read more >Managing DOM components with ReactDOM
Learn to expertly manage DOM components in a React app, including a deep dive into each ReactDOM method, with this comprehensive tutorial.
Read more >API
Render into a container which is appended to document.body . ... Tip: To get the root element of your rendered element, use container....
Read more >10 Rendering — HTML5
An element is being rendered if it is in a Document , either its parent node is itself ... embedded content is expected...
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 Free
Top 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
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript
Would work for part of that case (JavaScript turned off)
This way we can provide hydration by default. Since we take over your root, we can make assumptions about it, like, if it has any children we assume those were server side rendered and attempt to hydrate nodes.
EDIT: So, it’s not that the previous behavior was wrong, but it meant hydration was clumsy and you needed to pass a function (former @hyperapp/hydrate module) to the app() call. The current behavior means you just sit down and do nothing since it’s built-in. 😆