diff --git a/src/lib/Mermaid.svelte b/src/lib/Mermaid.svelte new file mode 100644 index 0000000..4e91293 --- /dev/null +++ b/src/lib/Mermaid.svelte @@ -0,0 +1,27 @@ + + +
+Mermaid graph + + diff --git a/src/routes/recipes/svelte-language-fundamentals/passing-data-between-component/+page.svx b/src/routes/recipes/svelte-language-fundamentals/passing-data-between-component/+page.svx new file mode 100644 index 0000000..be2999f --- /dev/null +++ b/src/routes/recipes/svelte-language-fundamentals/passing-data-between-component/+page.svx @@ -0,0 +1,545 @@ +--- +title: Passing data between components +layout: recipe +--- + + + +## 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 +
+ + + +
+ +## 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. + +
+ + +
+ +