svelte-mount Svelte Themes

Svelte Mount

A library to make detecting mounted state easier in Svelte

Svelte Mount

Svelte Mount provides both a store and component that signals whether a component is mounted.

Installing

This library is Svelte 5 only, if you want Svelte 3/4 use svelte-mount@2.

npm install svelte-mount -D

Using

To use you have two options, a component or a store:

  • Component

    This component can be incredibly useful if your content should only show once the component is mounted, such as using a animation with svelte kit or routify and only running the animation when the page is loaded

    <script>
        import { fly } from 'svelte/transition';
        import { Mount } from 'svelte-mount';
    </script>
    
    <Mount>
        <div in:fly="{{ y: -20 }}">Hello World</div>
    </Mount>
    
  • Store

    Below is an example of how you might want to be able to reactively check if your component is mounted or not and then use that information for something that requires it to be mounted. Thanks to Blu for this store

    <script>
        import { mounted } from 'svelte-mount';
    
        $effect(() => {
            if ($mounted) {
                console.log(document.getElementById('example').innerHTML);
            }
        });
    </script>
    
    <h1 id="example">Hello World</h1>
    

Support

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes