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.

Rendered view is appended to the `root` element instead of replacing its contents

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:18 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
zacenocommented, Sep 24, 2017

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript

Would work for part of that case (JavaScript turned off)

1reaction
jorgebucarancommented, Oct 6, 2017

@benchristel what influenced your decision to change it?

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. 😆

Read more comments on GitHub >

github_iconTop 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 >

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