Skip to content

Commit fe49dd3

Browse files
docs: Fix svelte playground example reactivity (#6311)
* docs: Fix svelte playground example reactivity * Rename next-js-app to react-next * Update pnpm lock * Fix prettier
1 parent d90dc8f commit fe49dd3

File tree

16 files changed

+334
-118
lines changed

16 files changed

+334
-118
lines changed

examples/svelte/playground/src/lib/stores.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ const initialList = [
2121

2222
export const list = writable(initialList)
2323
export const id = writable(initialId)
24+
25+
export type Todos = typeof initialList
26+
export type Todo = Todos[0]

examples/svelte/playground/src/routes/AddTodo.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
queryTimeMax,
77
list,
88
id,
9+
type Todo,
910
} from '../lib/stores'
1011
1112
const queryClient = useQueryClient()
1213
1314
let name = ''
1415
15-
const postTodo = async ({ name, notes }: { name: string; notes: string }) => {
16+
const postTodo = async ({ name, notes }: Omit<Todo, 'id'>) => {
1617
console.info('postTodo', { name, notes })
1718
return new Promise((resolve, reject) => {
1819
setTimeout(() => {

examples/svelte/playground/src/routes/App.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { useQueryClient } from '@tanstack/svelte-query'
3-
import Todos from './Todos.svelte'
3+
import TodosList from './TodosList.svelte'
44
import EditTodo from './EditTodo.svelte'
55
import AddTodo from './AddTodo.svelte'
66
import { views, editingIndex } from '../lib/stores'
@@ -19,7 +19,7 @@
1919

2020
{#each $views as view}
2121
<div>
22-
<Todos initialFilter={view} />
22+
<TodosList initialFilter={view} />
2323
<br />
2424
</div>
2525
{/each}

examples/svelte/playground/src/routes/EditTodo.svelte

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
queryTimeMax,
1111
list,
1212
editingIndex,
13-
} from '../lib/stores'
14-
15-
type Todo = { id: number; name: string; notes: string }
13+
type Todo,
14+
} from '$lib/stores'
15+
import { derived } from 'svelte/store'
1616
1717
const queryClient = useQueryClient()
1818
@@ -59,11 +59,13 @@
5959
})
6060
}
6161
62-
const query = createQuery({
63-
queryKey: ['todo', { id: $editingIndex }],
64-
queryFn: () => fetchTodoById({ id: $editingIndex || 0 }),
65-
enabled: $editingIndex !== null,
66-
})
62+
const query = createQuery(
63+
derived(editingIndex, ($editingIndex) => ({
64+
queryKey: ['todo', { id: $editingIndex }],
65+
queryFn: () => fetchTodoById({ id: $editingIndex || 0 }),
66+
enabled: $editingIndex !== null,
67+
})),
68+
)
6769
6870
const saveMutation = createMutation({
6971
mutationFn: patchTodo,

examples/svelte/playground/src/routes/Todos.svelte renamed to examples/svelte/playground/src/routes/TodosList.svelte

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
queryTimeMax,
77
list,
88
editingIndex,
9-
} from '../lib/stores'
9+
type Todos,
10+
} from '$lib/stores'
11+
import { derived, writable } from 'svelte/store'
1012
1113
export let initialFilter: string
1214
13-
let filter = initialFilter
14-
15-
const fetchTodos = async ({ filter }: { filter: string }) => {
16-
console.info('fetchTodos', { filter })
15+
let filter = writable(initialFilter)
1716
17+
const fetchTodos = async ({ filter }: { filter: string }): Promise<Todos> => {
1818
return new Promise((resolve, reject) => {
1919
setTimeout(() => {
2020
if (Math.random() < $errorRate) {
@@ -27,16 +27,18 @@
2727
})
2828
}
2929
30-
const query = createQuery<any>({
31-
queryKey: ['todos', { filter }],
32-
queryFn: () => fetchTodos({ filter }),
33-
})
30+
const query = createQuery(
31+
derived(filter, ($filter) => ({
32+
queryKey: ['todos', { filter: $filter }],
33+
queryFn: () => fetchTodos({ filter: $filter }),
34+
})),
35+
)
3436
</script>
3537

3638
<div>
3739
<label>
3840
Filter:{' '}
39-
<input bind:value={filter} />
41+
<input bind:value={$filter} />
4042
</label>
4143
</div>
4244

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)