Every Svelte application starts by imperatively creating a root component. On the client this component is mounted to a specific element. On the server, you want to get back a string of HTML instead which you can render. The following functions help you achieve those tasks.
mount Instantiates a component and mounts it to the given target:
import { mount ( alias ) function mount < Props extends Record < string , any > , Exports extends Record < string , any >> ( component : ComponentType < SvelteComponent < Props >> | Component < Props , Exports , any > , options : MountOptions < Props > ): Exports
import mount Mounts a component to the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component.
Transitions will play during the initial render unless the intro option is set to false.
} from 'svelte' ;
import App ( alias ) type App = SvelteComponent < Record < string , any > , any , any >
( alias ) const App : LegacyComponentType
import App from './App.svelte' ;
const app const app : {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > = mount ( alias ) mount < Record < string , any > , {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any >> ( component : ComponentType < SvelteComponent < Record < string , any > , any , any >> | Component < Record < string , any > , {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > , any > , options : MountOptions < ...> ): {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < ...>
import mount Mounts a component to the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component.
Transitions will play during the initial render unless the intro option is set to false.
( App ( alias ) const App : LegacyComponentType
import App , {
target ( property ) target : Document | Element | ShadowRoot Target element where the component will be mounted.
: document var document : Document window.document returns a reference to the document contained in the window.
MDN Reference
. querySelector ( method ) ParentNode . querySelector < Element > ( selectors : string ): Element | null ( + 4 overloads ) Returns the first element that is a descendant of node that matches selectors.
MDN Reference
( '#app' ),
props ( property ) props ? : Record < string , any > | undefined Component properties.
: { some ( property ) some : string : 'property' }
}); You can mount multiple components per page, and you can also mount from within your application, for example when creating a tooltip component and attaching it to the hovered element.
Note that unlike calling new App(...) in Svelte 4, things like effects (including onMount callbacks, and action functions) will not run during mount. If you need to force pending effects to run (in the context of a test, for example) you can do so with flushSync().
unmount Unmounts a component that was previously created with mount or hydrate .
If options.outro is true, transitions will play before the component is removed from the DOM:
import { mount ( alias ) function mount < Props extends Record < string , any > , Exports extends Record < string , any >> ( component : ComponentType < SvelteComponent < Props >> | Component < Props , Exports , any > , options : MountOptions < Props > ): Exports
import mount Mounts a component to the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component.
Transitions will play during the initial render unless the intro option is set to false.
, unmount ( alias ) function unmount ( component : Record < string , any > , options ? : {
outro ? : boolean ;
} | undefined ): Promise < void >
import unmount Unmounts a component that was previously mounted using mount or hydrate.
Since 5.13.0, if options.outro is true, transitions will play before the component is removed from the DOM.
Returns a Promise that resolves after transitions have completed if options.outro is true, or immediately otherwise (prior to 5.13.0, returns void).
import { mount , unmount } from 'svelte' ;
import App from './App.svelte' ;
const app = mount ( App , { target : document . body });
unmount ( app , { outro : true }); } from 'svelte' ;
import App ( alias ) type App = SvelteComponent < Record < string , any > , any , any >
( alias ) const App : LegacyComponentType
import App from './App.svelte' ;
const app const app : {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > = mount ( alias ) mount < Record < string , any > , {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any >> ( component : ComponentType < SvelteComponent < Record < string , any > , any , any >> | Component < Record < string , any > , {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > , any > , options : MountOptions < ...> ): {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < ...>
import mount Mounts a component to the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component.
Transitions will play during the initial render unless the intro option is set to false.
( App ( alias ) const App : LegacyComponentType
import App , { target ( property ) target : Document | Element | ShadowRoot Target element where the component will be mounted.
: document var document : Document window.document returns a reference to the document contained in the window.
MDN Reference
. body ( property ) Document . body : HTMLElement The Document.body property represents the
or node of the current document, or null if no such element exists.
MDN Reference
});
unmount ( alias ) unmount ( component : Record < string , any > , options ? : {
outro ? : boolean ;
} | undefined ): Promise < void >
import unmount Unmounts a component that was previously mounted using mount or hydrate.
Since 5.13.0, if options.outro is true, transitions will play before the component is removed from the DOM.
Returns a Promise that resolves after transitions have completed if options.outro is true, or immediately otherwise (prior to 5.13.0, returns void).
import { mount , unmount } from 'svelte' ;
import App from './App.svelte' ;
const app = mount ( App , { target : document . body });
unmount ( app , { outro : true }); ( app const app : {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > , { outro ( property ) outro ? : boolean | undefined : true }); Returns a Promise that resolves after transitions have completed if options.outro is true, or immediately otherwise.
render Only available on the server and when compiling with the server option. Takes a component and returns an object with body and head properties on it, which you can use to populate the HTML when server-rendering your app:
import { render ( alias ) function render < Comp extends SvelteComponent < any > | Component < any > , Props extends ComponentProps < Comp > = ComponentProps < Comp >> ( ... args : {} extends Props ? [ component : Comp extends SvelteComponent < any > ? ComponentType < Comp > : Comp , options ? : {
props ? : Omit < Props , "$$slots" | "$$events" > ;
context ? : Map < any , any > ;
idPrefix ? : string ;
csp ? : Csp ;
transformError ? : ( error : unknown ) => unknown | Promise < unknown > ;
}] : [ component : Comp extends SvelteComponent < any > ? ComponentType < Comp > : Comp , options : {
props : Omit < Props , "$$slots" | "$$events" > ;
context ? : Map < any , any > ;
idPrefix ? : string ;
csp ? : Csp ;
transformError ? : ( error : unknown ) => unknown | Promise < unknown > ;
}]): RenderOutput
import render Only available on the server and when compiling with the server option.
Takes a component and returns an object with body and head properties on it, which you can use to populate the HTML when server-rendering your app.
} from 'svelte/server' ;
import App ( alias ) type App = SvelteComponent < Record < string , any > , any , any >
( alias ) const App : LegacyComponentType
import App from './App.svelte' ;
const result const result : RenderOutput = render ( alias ) render < SvelteComponent < Record < string , any > , any , any > , Record < string , any >> ( component : ComponentType < SvelteComponent < Record < string , any > , any , any >> , options ? : {
props ? : Omit < Record < string , any > , "$$slots" | "$$events" > | undefined ;
context ? : Map < any , any > ;
idPrefix ? : string ;
csp ? : Csp ;
transformError ? : (( error : unknown ) => unknown | Promise < unknown > ) | undefined ;
} | undefined ): RenderOutput
import render Only available on the server and when compiling with the server option.
Takes a component and returns an object with body and head properties on it, which you can use to populate the HTML when server-rendering your app.
( App ( alias ) const App : LegacyComponentType
import App , {
props ( property ) props ? : Omit < Record < string , any > , "$$slots" | "$$events" > | undefined : { some ( property ) some : string : 'property' }
});
result const result : RenderOutput . body ( property ) SyncRenderOutput . body : string HTML that goes somewhere into the <body>
;
result const result : RenderOutput . head ( property ) SyncRenderOutput . head : string HTML that goes into the <head>
; hydrate Like mount, but will reuse up any HTML rendered by Svelte’s SSR output (from the render function) inside the target and make it interactive:
import { hydrate ( alias ) function hydrate < Props extends Record < string , any > , Exports extends Record < string , any >> ( component : ComponentType < SvelteComponent < Props >> | Component < Props , Exports , any > , options : {} extends Props ? {
target : Document | Element | ShadowRoot ;
props ? : Props ;
events ? : Record < string , ( e : any ) => any > ;
context ? : Map < any , any > ;
intro ? : boolean ;
recover ? : boolean ;
transformError ? : ( error : unknown ) => unknown ;
} : {
target : Document | Element | ShadowRoot ;
props : Props ;
events ? : Record < string , ( e : any ) => any > ;
context ? : Map < any , any > ;
intro ? : boolean ;
recover ? : boolean ;
transformError ? : ( error : unknown ) => unknown ;
}): Exports
import hydrate Hydrates a component on the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component
} from 'svelte' ;
import App ( alias ) type App = SvelteComponent < Record < string , any > , any , any >
( alias ) const App : LegacyComponentType
import App from './App.svelte' ;
const app const app : {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > = hydrate ( alias ) hydrate < Record < string , any > , {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any >> ( component : ComponentType < SvelteComponent < Record < string , any > , any , any >> | Component < Record < string , any > , {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < string , any > , any > , options : {
... ;
}): {
$on ? ( type : string , callback : ( e : any ) => void ): () => void ;
$set ? ( props : Partial < Record < string , any >> ): void ;
} & Record < ...>
import hydrate Hydrates a component on the given target and returns the exports and potentially the props (if compiled with accessors: true) of the component
( App ( alias ) const App : LegacyComponentType
import App , {
target ( property ) target : Document | Element | ShadowRoot : document var document : Document window.document returns a reference to the document contained in the window.
MDN Reference
. querySelector ( method ) ParentNode . querySelector < Element > ( selectors : string ): Element | null ( + 4 overloads ) Returns the first element that is a descendant of node that matches selectors.
MDN Reference
( '#app' ),
props ( property ) props ? : Record < string , any > | undefined : { some ( property ) some : string : 'property' }
}); As with mount, effects will not run during hydrate — use flushSync() immediately afterwards if you need them to.