Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Svelte Conditional Component rendering

Tags:

svelte

I have a basic if/else in the app to show either a preloader or a component.

{#if preloading}
    <video />
{:else}
    <svelte:component this={component} on:preload={preload}/>
{/if}

It starts with displaying the component correctly, when the preload flag is set, it shows the video, but it shows it over the svelte:component rather than replacing it.

I tried using {#key} to force a rerender by using a number as an id and incrementing it when preload is set, but the component is still there.

Any ideas?

like image 745
M.Alnashmi Avatar asked Oct 29 '25 22:10

M.Alnashmi


1 Answers

Try doing it like in the repl example:

<script>
  import video from "./somewhere.svelte"
  import component from "./somewhereelse.svelte" 
</script>

<svelte:component this={preloading ? video : component} on:preload={preload}/>
like image 95
Gh05d Avatar answered Nov 03 '25 00:11

Gh05d