Component destroy event #552
Replies: 2 comments 2 replies
-
A callback returned from the <div
x-data="{open: false}"
x-init="() => {
if (window.pikaday) {
return
}
window.pikaday = new Pikaday({ field: el })
return () => {
if(window.pikaday) window.pikaday.destroy()
}
}"
>
<button x-on:click.prevent="open = !open">
<div x-if="open">
<input type="text" x-data x-ref="input" x-init="initPikaday($refs.input)">
</div>
</div> |
Beta Was this translation helpful? Give feedback.
-
There's no destroy callback in Alpine. You can use $watch (https://github.com/alpinejs/alpine#watch) to check when open becomes false. Note, there are a few bugs in that snippet: you never set pikaday to false after calling destroy, you use x-if on a div rather then template, etc. To be fair, I would use x-show (with x-if you have to fiddle with nextTicks and/or timeouts) and encapsulate all the logic in your component. https://codepen.io/SimoTod/pen/jOWEvjx |
Beta Was this translation helpful? Give feedback.
-
Does there exist an destroy event that gets fired when an alpine component is removed?
I'm using Pikaday and it is creating a div for each initiation that could be removed using a .destroy() method
Here is a basic example
How would i call the
destroyPikaday()
function when the nested Alpine component is removed / destroyed?Beta Was this translation helpful? Give feedback.
All reactions