OnMount
#397
Replies: 2 comments
-
For now this workaround does the job: const withMount = (element, onMount) => {
// Try immediate check first
if (element.isConnected) {
onMount(element)
return element
}
// Fall back to observer if not immediately connected
const observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node === element) {
onMount(element)
observer.disconnect()
return
}
}
}
})
observer.observe(document.body, {
childList: true,
subtree: true
})
// Also check next frame in case we missed it
requestAnimationFrame(() => {
if (element.isConnected) {
onMount(element)
observer.disconnect()
}
})
return element
}
const MountExample = () => {
const canvasEl = canvas({
width: 200,
height: 200,
style: 'background:orange;position:absolute;border:3px solid red;'
})
return div(
withMount(canvasEl, (el) => {
prnt('mounted!!', el)
// Do WebGL initialization etc
})
)
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
See #376 for a detailed discussion of the topic (the thread talks about |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I'm looking for an elegant way to do stuff to an element after it's been added to dom. Currently I wait with timer and document.getElementByID('webglCanvasElement') but want something more direct. Is there a way to do something like this with vanJS or regular JS? Basically I want a signal once the element has been added to dom so I can get reference to it.
Beta Was this translation helpful? Give feedback.
All reactions