Skip to content

Commit

Permalink
Fix: SvelteKit post-handlers tutorial (#1085)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Jan 6, 2025
1 parent 375fa3b commit 5c49543
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
let { data = $bindable() } = $props();
let { data } = $props();
</script>

<div class="centered">
Expand All @@ -26,11 +26,13 @@

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

data.todos = [...data.todos, {
const todos = [...data.todos, {
id,
description
}];

data = { ...data, todos };

input.value = '';
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ We're returning a response with a [201 Created](https://http.dog/201) status and
+++ const { id } = await response.json();
data.todos = [...data.todos, {
const todos = [...data.todos, {
id,
description
}];+++
}];
data = { ...data, todos };+++
input.value = '';
}}
/>
```

> [!NOTE] You should only mutate `data` in such a way that you'd get the same result by reloading the page.
> [!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 comments on commit 5c49543

Please sign in to comment.