Skip to content

Commit

Permalink
Merge pull request #2432 from sveltejs/gh-2194
Browse files Browse the repository at this point in the history
Update a few examples
  • Loading branch information
Rich-Harris authored Apr 16, 2019
2 parents 7ad6349 + bb10298 commit e7f5467
Show file tree
Hide file tree
Showing 21 changed files with 673 additions and 712 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import { name, greeting } from './stores.js';
</script>

<h1>{$greeting}</h1>
<input value={$name}>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { writable, derived } from 'svelte/store';

export const name = writable('world');

export const greeting = derived(
name,
$name => `Hello ${$name}!`
);
10 changes: 10 additions & 0 deletions site/content/tutorial/08-stores/06-store-bindings/app-b/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { name, greeting } from './stores.js';
</script>

<h1>{$greeting}</h1>
<input bind:value={$name}>

<button on:click="{() => $name += '!'}">
Add exclamation mark!
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { writable, derived } from 'svelte/store';

export const name = writable('world');

export const greeting = derived(
name,
$name => `Hello ${$name}!`
);
23 changes: 23 additions & 0 deletions site/content/tutorial/08-stores/06-store-bindings/text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Store bindings
---

If a store is writable — i.e. it has a `set` method — you can bind to its value, just as you can bind to local component state.

In this example we have a writable store `name` and a derived store `greeting`. Update the `<input>` element:

```html
<input bind:value={$name}>
```

Changing the input value will now update `name` and all its dependents.

We can also assign directly to store values inside a component. Add a `<button>` element:

```html
<button on:click="{() => $name += '!'}">
Add exclamation mark!
</button>
```

The `$name += '!'` assignment is equivalent to `name.set($name + '!')`.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e7f5467

Please sign in to comment.