Skip to content

Memoized elements are removed & added anyway #77

@timwis

Description

@timwis

Hello, I'd like to cross-post the issue shama/on-load#10 as @shama and @yoshuawuyts have suggested the issue may reside in morphdom.

I reported that:

if you memoize your el, the onload callback gets called at every re-render.

@shama investigated, reproduced the issue, and diagnosed that:

For some reason each call to yo.update() is quickly removing and adding the same node. We could fix this here by queueing events, so if an load is called immediately after an unload, it ignores it. But morphdom shouldn't be updating the node in the DOM to begin with.

The following code sample illustrates the issue, with a live demo here:

var html = require('bel')
var morphdom = require('morphdom')

var saved
function child () {
  saved = saved || html`<h1 onload=${() => console.log('child on')} onunload=${() => console.log('child off')}>child</h1>`
  return saved
}

function parent () {
  return html`<div>${child()}</div>`
}

var root = parent()
document.body.appendChild(root)
setTimeout(() => {
  console.log('updating')
  root = morphdom(root, parent())
}, 1000)

Expected behaviour: child on only gets called once, when the element is mounted.

Actual behaviour: child on and child off are also called at re-render, even though the child element was already memoized.

This issue prevents things like memoizing leaflet/d3 components so they don't have to be re-initialized at every state change. Any suggestions on how we might resolve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions