Skip to content

Commit 5c49543

Browse files
Fix: SvelteKit post-handlers tutorial (sveltejs#1085)
* Fix tutorial to work with immutable data prop * Remove extraneous $bindable rune on data prop * Update apps/svelte.dev/content/tutorial/03-sveltekit/07-api-routes/02-post-handlers/index.md --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 375fa3b commit 5c49543

File tree

2 files changed

+9
-5
lines changed
  • apps/svelte.dev/content/tutorial/03-sveltekit/07-api-routes/02-post-handlers

2 files changed

+9
-5
lines changed

apps/svelte.dev/content/tutorial/03-sveltekit/07-api-routes/02-post-handlers/+assets/app-b/src/routes/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
let { data = $bindable() } = $props();
2+
let { data } = $props();
33
</script>
44

55
<div class="centered">
@@ -26,11 +26,13 @@
2626

2727
const { id } = await response.json();
2828

29-
data.todos = [...data.todos, {
29+
const todos = [...data.todos, {
3030
id,
3131
description
3232
}];
3333

34+
data = { ...data, todos };
35+
3436
input.value = '';
3537
}}
3638
/>

apps/svelte.dev/content/tutorial/03-sveltekit/07-api-routes/02-post-handlers/index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ We're returning a response with a [201 Created](https://http.dog/201) status and
7474
7575
+++ const { id } = await response.json();
7676
77-
data.todos = [...data.todos, {
77+
const todos = [...data.todos, {
7878
id,
7979
description
80-
}];+++
80+
}];
81+
82+
data = { ...data, todos };+++
8183
8284
input.value = '';
8385
}}
8486
/>
8587
```
8688

87-
> [!NOTE] You should only mutate `data` in such a way that you'd get the same result by reloading the page.
89+
> [!NOTE] You should only update `data` in such a way that you'd get the same result by reloading the page. The `data` prop is not _deeply_ reactive, so you need to replace it — mutations like `data.todos = todos` will not cause a re-render.

0 commit comments

Comments
 (0)