Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the contents of a target in Svelte instead of appending to children?

Calling new Component({ target }) appends component to target, I want to replace all the old content of the target with the new component. How can I do that?

like image 628
Palash Chordia Avatar asked Oct 20 '25 02:10

Palash Chordia


1 Answers

If you're hydrating server-rendered markup, it's better to use the hydrate: true option (docs here).

If target was previously occupied by a Svelte component, it's better to call that component's $destroy() method.

Otherwise, the simplest way is to just empty out the target element:

target.innerHTML = '';
new Component({ target });
like image 146
Rich Harris Avatar answered Oct 22 '25 04:10

Rich Harris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!