Passing data between components

Stores

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Any

Stores allow components to “react” when the content of the store is changed.

If the store is declared “globally” (not in a components) any scripts can access it and interact with it.

Globally declared store
In component declared store
You are creating a hard/direct dependency between Component1.svelte and Component2.svelte.
Component2.svelte can't exist without Component1.svelte

Global variable

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Any

Similar to store, a global variable act as a central point of storage. But global variable are not reactive, so you have to implement yourself a method to update the component.

Svelte event

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Child → Parent

The data sharing only works in the direction child to parent.

get/setContext

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Parent → Children

(Can be bi-directional and reactive, see below)


The data sharing only works in the direction parent to children (and children of children).

Context with normal variable
Context with store
Bi-directional and reactive

Props

One-way binding (down)

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Parent → Child

The parent is providing the value to its child. Changing the value in the parent will be reflected in the child.

Two-way binding

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Parent ↔ Child

The parent is providing a variable that can be written by the child. Changes made in the parent will be reflected and the child, changes made in children will be send to its parent

One-way binding (up)

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Child → Parent

The parent is providing a function that will update the parent scope from its children

DOM event

In Component In Javascript/TypeScript Reactive Bi-directional Direction
Child → Parents

Use native DOMEvent to bubble up data from a child to any parent willing to listen to it.

Published:
Last update: