Skip to main content

svelte/reactivity

Svelte provides reactive versions of various built-ins like SvelteMap, SvelteSet and SvelteURL. These can be imported from svelte/reactivity and used just like their native counterparts.

<script>
	import { SvelteURL } from 'svelte/reactivity';

	const url = new SvelteURL('https://example.com/path');
</script>

<!-- changes to these... -->
<input bind:value={url.protocol} />
<input bind:value={url.hostname} />
<input bind:value={url.pathname} />

<hr />

<!-- will update `href` and vice versa -->
<input bind:value={url.href} />
import {
	import MediaQueryMediaQuery,
	class SvelteDateSvelteDate,
	class SvelteMap<K, V>SvelteMap,
	class SvelteSet<T>SvelteSet,
	class SvelteURLSvelteURL,
	class SvelteURLSearchParamsSvelteURLSearchParams,
	import createSubscribercreateSubscriber
} from 'svelte/reactivity';

MediaQuery

Available since 5.7.0

Creates a media query and provides a current property that reflects whether or not it matches.

Use it carefully — during server-side rendering, there is no way to know what the correct value should be, potentially causing content to change upon hydration. If you can use the media query in CSS to achieve the same effect, do that.

<script>
	import { MediaQuery } from 'svelte/reactivity';

	const large = new MediaQuery('min-width: 800px');
</script>

<h1>{large.current ? 'large screen' : 'small screen'}</h1>
class MediaQuery extends ReactiveValue<boolean> {}
constructor(query: string, fallback?: boolean | undefined);
  • query A media query string
  • fallback Fallback value for the server

SvelteDate

class SvelteDate extends Date {}
constructor(...params: any[]);
#private;

SvelteMap

class SvelteMap<K, V> extends Map<K, V> {}
constructor(value?: Iterable<readonly [K, V]> | null | undefined);
set(key: K, value: V): this;
#private;

SvelteSet

class SvelteSet<T> extends Set<T> {}
constructor(value?: Iterable<T> | null | undefined);
add(value: T): this;
#private;

SvelteURL

class SvelteURL extends URL {}
get searchParams(): SvelteURLSearchParams;
#private;

SvelteURLSearchParams

class SvelteURLSearchParams extends URLSearchParams {}
[REPLACE](params: URLSearchParams): void;
#private;

createSubscriber

Available since 5.7.0

Returns a subscribe function that, if called in an effect (including expressions in the template), calls its start callback with an update function. Whenever update is called, the effect re-runs.

If start returns a function, it will be called when the effect is destroyed.

If subscribe is called in multiple effects, start will only be called once as long as the effects are active, and the returned teardown function will only be called when all effects are destroyed.

It’s best understood with an example. Here’s an implementation of MediaQuery:

import { import createSubscribercreateSubscriber } from 'svelte/reactivity';
import { function on<Type extends keyof WindowEventMap>(window: Window, type: Type, handler: (this: Window, event: WindowEventMap[Type]) => any, options?: AddEventListenerOptions | undefined): () => void (+4 overloads)

Attaches an event handler to the window and returns a function that removes the handler. Using this rather than addEventListener will preserve the correct order relative to handlers added declaratively (with attributes like onclick), which use event delegation for performance reasons

on
} from 'svelte/events';
export class class MediaQueryMediaQuery { #query; #subscribe; constructor(query: anyquery) { this.#query = var window: Window & typeof globalThiswindow.function matchMedia(query: string): MediaQueryListmatchMedia(`(${query: anyquery})`); this.#subscribe = import createSubscribercreateSubscriber((update: anyupdate) => { // when the `change` event occurs, re-run any effects that read `this.current` const const off: () => voidoff = on<MediaQueryList, "change">(element: MediaQueryList, type: "change", handler: (this: MediaQueryList, event: MediaQueryListEvent) => any, options?: AddEventListenerOptions | undefined): () => void (+4 overloads)

Attaches an event handler to an element and returns a function that removes the handler. Using this rather than addEventListener will preserve the correct order relative to handlers added declaratively (with attributes like onclick), which use event delegation for performance reasons

on
(this.#query, 'change', update: anyupdate);
// stop listening when all the effects are destroyed return () => const off: () => voidoff(); }); } get MediaQuery.current: booleancurrent() { this.#subscribe(); // Return the current state of the query, whether or not we're in an effect return this.#query.MediaQueryList.matches: booleanmatches; } }
function createSubscriber(
	start: (update: () => void) => (() => void) | void
): () => void;

Edit this page on GitHub