Spec Registry
Help your agents use open-source better. Learn more.
Find usage specs for your project’s dependencies
- Author
- tessl
- Last updated
- Spec files
npm-svelte
Describes: npm/svelte
- Description
- A cybernetically enhanced web application framework that compiles to highly optimized JavaScript with reactive state management and component-based architecture.
- Author
- tessl
- Last updated
lifecycle.md docs/
1# Component Lifecycle23Svelte provides lifecycle functions that allow you to run code at specific points in a component's life cycle. These functions must be called during component initialization.45## Capabilities67### onMount89Schedules a function to run as soon as the component has been mounted to the DOM. Unlike `$effect`, the provided function only runs once.1011```typescript { .api }12/**13* Schedules a function to run as soon as the component has been mounted to the DOM14* @param fn - Function to run on mount, can return cleanup function15*/16function onMount<T>(fn: () => NotFunction<T> | Promise<NotFunction<T>> | (() => any)): void;17```1819**Usage Examples:**2021```typescript22import { onMount } from "svelte";2324let data = [];2526onMount(async () => {27// Fetch data when component mounts28const response = await fetch("/api/data");29data = await response.json();3031// Return cleanup function (optional)32return () => {33console.log("Component unmounted");34};35});3637// Setup intervals or event listeners38onMount(() => {39const interval = setInterval(() => {40console.log("Timer tick");41}, 1000);4243// Cleanup interval when component unmounts44return () => clearInterval(interval);45});46```4748### onDestroy4950Schedules a callback to run immediately before the component is unmounted. This is the only lifecycle function that runs inside server-side components.5152```typescript { .api }53/**54* Schedules a callback to run immediately before the component is unmounted55* @param fn - Function to run on component destruction56*/57function onDestroy(fn: () => any): void;58```5960**Usage Examples:**6162```typescript63import { onDestroy } from "svelte";6465let subscription;6667// Subscribe to external service68subscription = externalService.subscribe(handleData);6970onDestroy(() => {71// Clean up subscription72if (subscription) {73subscription.unsubscribe();74}75});7677// Alternative pattern - cleanup in onMount78import { onMount, onDestroy } from "svelte";7980onMount(() => {81const cleanup = setupSomething();82onDestroy(cleanup);83});84```8586### beforeUpdate (Deprecated)8788Schedules a callback to run immediately before the component is updated after any state change. **Deprecated in Svelte 5** - use `$effect.pre` instead.8990```typescript { .api }91/**92* @deprecated Use $effect.pre instead93* Schedules a callback to run immediately before the component is updated94* @param fn - Function to run before updates95*/96function beforeUpdate(fn: () => void): void;97```9899**Usage Examples:**100101```typescript102import { beforeUpdate } from "svelte";103104let div;105let autoscroll;106107beforeUpdate(() => {108if (div) {109const scrollableDistance = div.scrollHeight - div.offsetHeight;110autoscroll = div.scrollTop > (scrollableDistance - 20);111}112});113```114115### afterUpdate (Deprecated)116117Schedules a callback to run immediately after the component has been updated. **Deprecated in Svelte 5** - use `$effect` instead.118119```typescript { .api }120/**121* @deprecated Use $effect instead122* Schedules a callback to run immediately after the component has been updated123* @param fn - Function to run after updates124*/125function afterUpdate(fn: () => void): void;126```127128**Usage Examples:**129130```typescript131import { afterUpdate } from "svelte";132133let div;134let autoscroll = false;135136afterUpdate(() => {137if (autoscroll && div) {138div.scrollTo(0, div.scrollHeight);139}140});141```142143## Types144145```typescript { .api }146type NotFunction<T> = T extends Function ? never : T;147```148149## Migration Notes150151In Svelte 5, prefer using runes over the deprecated lifecycle functions:152153- Use `$effect(() => { ... })` instead of `afterUpdate`154- Use `$effect.pre(() => { ... })` instead of `beforeUpdate`155- `onMount` and `onDestroy` remain the preferred way to handle component mounting and cleanup