Skip to content

Commit 6863298

Browse files
committed
Merge remote-tracking branch 'sveltejs/main' into update-up-to-20230316
2 parents 65e99d4 + d5b80c5 commit 6863298

File tree

17 files changed

+125
-140
lines changed

17 files changed

+125
-140
lines changed

content/tutorial/01-svelte/04-logic/04-each-blocks/README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ title: Each blocks
66

77
```svelte
88
<ul>
9-
{#each cats as cat}
10-
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
11-
{cat.name}
12-
</a></li>
13-
{/each}
9+
+++{#each cats as cat}+++
10+
<li>
11+
<a href="https://www.youtube.com/watch?v={cat.id}">
12+
{cat.name}
13+
</a>
14+
</li>
15+
+++{/each}+++
1416
</ul>
1517
```
1618

@@ -19,10 +21,12 @@ title: Each blocks
1921
第2引数として現在の *index* をこのように取得することができます。
2022

2123
```svelte
22-
{#each cats as cat, i}
23-
<li><a target="_blank" href="https://www.youtube.com/watch?v={cat.id}">
24-
{i + 1}: {cat.name}
25-
</a></li>
24+
{#each cats as cat+++, i}+++
25+
<li>
26+
<a href="https://www.youtube.com/watch?v={cat.id}">
27+
+++{i + 1}:+++ {cat.name}
28+
</a>
29+
</li>
2630
{/each}
2731
```
2832

content/tutorial/01-svelte/04-logic/04-each-blocks/app-a/src/lib/App.svelte

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919

2020
<ul>
2121
<!-- open each block -->
22-
<li>
23-
<a
24-
target="_blank"
25-
href="https://www.youtube.com/watch?v={cat.id}"
26-
>
27-
{cat.name}
28-
</a>
29-
</li>
22+
<li>
23+
<a href="https://www.youtube.com/watch?v={cat.id}">
24+
{cat.name}
25+
</a>
26+
</li>
3027
<!-- close each block -->
3128
</ul>

content/tutorial/01-svelte/04-logic/04-each-blocks/app-b/src/lib/App.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
<ul>
2121
{#each cats as { id, name }, i}
2222
<li>
23-
<a
24-
target="_blank"
25-
href="https://www.youtube.com/watch?v={id}"
26-
>
23+
<a href="https://www.youtube.com/watch?v={id}">
2724
{i + 1}: {name}
2825
</a>
2926
</li>

content/tutorial/01-svelte/04-logic/05-keyed-each-blocks/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ title: Keyed each blocks
44

55
デフォルトでは、`each` ブロックの値を変更すると、ブロックの *末尾* にアイテムを追加・削除し、変更された値を更新します。これはあなたが望むものではないかもしれません。
66

7-
説明するより見ていただいたほうがわかりやすいしょう。'Remove first thing' ボタンを何度かクリックして、何が起きるか確認してください。先頭の `<Thing>` コンポーネントは削除されず、*末尾の* DOM ノードが削除されます。そして残りの DOM ノードの `name` の値は更新されますが、絵文字は更新されません。
7+
説明するより見ていただいたほうがわかりやすいしょう。'Remove first thing' ボタンを何度かクリックして、何が起きるか確認してください。先頭の `<Thing>` コンポーネントは削除されず、*末尾の* DOM ノードが削除されます。そして残りの DOM ノードの `name` の値は更新されますが、絵文字は更新されません。この絵文字は、`Thing.svelte` が初期化されるときに固定されているのです。
88

99
代わりに、先頭の `<Thing>` コンポーネントとその DOM ノードだけを削除して、残りには影響を与えないようにしたいと思います。
1010

1111
そのためには、`each` ブロックに一意な識別子 (または"key") を指定します。
1212

1313
```svelte
14-
{#each things as thing (thing.id)}
14+
{#each things as thing (+++thing.id+++)}
1515
<Thing name={thing.name}/>
1616
{/each}
1717
```

content/tutorial/01-svelte/04-logic/05-keyed-each-blocks/app-a/src/lib/Thing.svelte

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script>
2-
import { onDestroy } from 'svelte';
3-
42
const emojis = {
53
apple: '🍎',
64
banana: '🍌',
@@ -12,13 +10,9 @@
1210
// the name is updated whenever the prop value changes...
1311
export let name;
1412
15-
// ...but the "emoji" variable is fixed upon initialisation of the component
13+
// ...but the "emoji" variable is fixed upon initialisation
14+
// of the component because it uses `const` instead of `$:`
1615
const emoji = emojis[name];
17-
18-
// observe in the console which entry is removed
19-
onDestroy(() => {
20-
console.log('thing destroyed: ' + name);
21-
});
2216
</script>
2317

2418
<p>

content/tutorial/01-svelte/04-logic/05-keyed-each-blocks/app-b/src/lib/Thing.svelte

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script>
22
async function getRandomNumber() {
3-
const res = await fetch(
4-
`https://svelte.dev/tutorial/random-number`
5-
);
6-
const text = await res.text();
3+
// Fetch a random number between 0 and 100
4+
// (with a delay, so that we can see it)
5+
const res = await fetch('/random-number');
76
87
if (res.ok) {
9-
return text;
8+
return await res.text();
109
} else {
11-
throw new Error(text);
10+
// Sometimes the API will fail!
11+
throw new Error('Request failed');
1212
}
1313
}
1414
@@ -19,7 +19,9 @@
1919
}
2020
</script>
2121

22-
<button on:click={handleClick}> generate random number </button>
22+
<button on:click={handleClick}>
23+
generate random number
24+
</button>
2325

2426
<!-- replace this element -->
2527
<p>{promise}</p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export async function GET(req) {
2+
const query = req.url.searchParams;
3+
let min = query.get('min') || '0';
4+
let max = query.get('max') || '100';
5+
min = +min;
6+
max = +max;
7+
8+
// simulate a long delay
9+
await new Promise((res) => setTimeout(res, 1000));
10+
11+
// fail sometimes
12+
if (Math.random() < 0.333) {
13+
return new Response(`Failed to generate random number. Please try again`, {
14+
status: 400,
15+
headers: { 'Access-Control-Allow-Origin': '*' }
16+
});
17+
}
18+
19+
const num = min + Math.round(Math.random() * (max - min));
20+
return new Response(String(num), {
21+
headers: { 'Access-Control-Allow-Origin': '*' }
22+
});
23+
}

content/tutorial/01-svelte/04-logic/06-await-blocks/app-b/src/lib/App.svelte

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<script>
22
async function getRandomNumber() {
3-
const res = await fetch(
4-
`https://svelte.dev/tutorial/random-number`
5-
);
6-
const text = await res.text();
3+
// Fetch a random number between 0 and 100
4+
// (with a delay, so that we can see it)
5+
const res = await fetch('/random-number');
76
87
if (res.ok) {
9-
return text;
8+
return await res.text();
109
} else {
11-
throw new Error(text);
10+
// Sometimes the API will fail!
11+
throw new Error('Request failed');
1212
}
1313
}
1414
@@ -19,7 +19,9 @@
1919
}
2020
</script>
2121

22-
<button on:click={handleClick}> generate random number </button>
22+
<button on:click={handleClick}>
23+
generate random number
24+
</button>
2325

2426
{#await promise}
2527
<p>...waiting</p>

content/tutorial/01-svelte/05-events/01-dom-events/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: DOM events
55
今までざっと見てきたように、`on:` ディレクティブを使用して要素の任意のイベントをリスニングできます。
66

77
```svelte
8-
<div on:mousemove={handleMousemove}>
8+
<div +++on:mousemove={handleMousemove}+++>
99
The mouse position is {m.x} x {m.y}
1010
</div>
1111
```

0 commit comments

Comments
 (0)