-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memoized elements trigger onload on every render #10
Comments
Hmm weird, I can repro this but not sure why. I'll keep looking into a fix. |
It's interesting, given this example: var saved = null
function parent () {
saved = saved || onload(yo`<div>parent</div>`, function () {
results.push('parent on')
}, function () {
results.push('parent off')
})
return saved
}
function app () {
return yo`<div>${parent()}</div>`
}
var root = app()
document.body.appendChild(root)
setInterval(function () {
root = yo.update(root, app())
}, 1000) For some reason each call to We should look into a fix on morphdom or see how @yoshuawuyts's https://github.com/yoshuawuyts/nanodiff pans out. Or try diffhtml again. As a better way to fix this. |
Quick reply (I'm about to head out again): I tried minifying |
@yoshuawuyts @shama I've started drafting tests for expected functionality. Is this format helpful? Or would pseudo code or requirebins be better? Happy to add more if you can verbally describe them. |
@timwis looking neat! |
@timwis Cool! Yep those are helpful. We could even add choo directly related tests here if you want. I can give you commit access. The test.js file is at the point where it should be split into it's own folder. We can start adding choo related test files there and just |
Hey guys,
Does morphdom v2 help this issue? (As far as I know, we can't really use things like maps in choo without this because |
Update: just tested your test case above with morphdom 2 and I'm seeing the same result :-/ |
Updated test case to work with const html = require('choo/html')
const choo = require('choo')
const app = choo()
app.model({
state: {
count: 0,
toggled: true
},
reducers: {
update: (data, state) => ({ count: state.count + 1 }),
toggle: (data, state) => ({ toggled: !state.toggled })
}
})
const view = (state, prev, send) => {
return (state.toggled)
? html`
<div>
<h1>count: ${state.count}</h1>
<button onclick=${() => send('update')}>Update state</button>
<button onclick=${() => send('toggle')}>Toggle memo</button>
${subView(state, prev, send)}
${memoizedSubView(state, prev, send)}
</div>`
: html`
<div>
<h1>count: ${state.count}</h1>
<button onclick=${() => send('update', 'world')}>Update state</button>
<button onclick=${() => send('toggle')}>Toggle memo</button>
${subView(state, prev, send)}
</div>`
}
function subView (state, prev, send) {
return html`
<div onload=${(el) => console.log('subView loaded')}>
subView
</div>
`
}
let memoizedEl = null
let mounted = false
function memoizedSubView (state, prev, send) {
if (!memoizedEl) {
memoizedEl = html`
<div
onunload=${(el) => {
console.log('memoizedSubView unloaded')
mounted = false
}}
onload=${(el) => console.log('memoizedSubView loaded')}>
memoizedSubView
</div>
`
mounted = true
return memoizedEl
} else if (!mounted) {
mounted = true
return memoizedEl
} else {
const placeholder = html`<template></template>`
placeholder.isSameNode = (el) => el.isSameNode(memoizedEl)
return placeholder
}
}
app.router(['/', view])
const tree = app.start()
document.body.appendChild(tree) Tested and works 🎉🎉🎉🎉🎉🎉 edit: published a playground with the patched dependencies checked in ✨ |
I've noticed that if you memoize your el, the
onload
callback gets called at every re-render. I've written up a demo that compares a non-memoized sub view to a memoized sub view.Any idea why?
The text was updated successfully, but these errors were encountered: