Skip to content

Commit 2d308a1

Browse files
elliott-with-the-longest-name-on-githubautofix-ci[bot]lachlancollins
committed
feat(svelte-query): Improve svelte runes API (#8852)
* feat: Draft proposal * chore: Improve reactive containers * ci: apply automated fixes * oops * fix: Update API, add a bunch of tests * merge main * fix: use const * more tests * feat: More tests, back to thunks, fixed svelte-query-persist-client * feat: More tests and examples! * lockfile * fixes * Fix current CI errors * More small fixes/tweaks * Remove test.only * ci: apply automated fixes * Fix pnpm-lock, fix import order * update main docs * feat: More tests * ci: apply automated fixes * add back old tests * Cleanup * Fix persist client * Fix useMutationState --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com>
1 parent 342e140 commit 2d308a1

File tree

77 files changed

+3691
-1331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+3691
-1331
lines changed

docs/config.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@
127127
{
128128
"label": "SSR & SvelteKit",
129129
"to": "framework/svelte/ssr"
130-
},
131-
{
132-
"label": "Reactivity",
133-
"to": "framework/svelte/reactivity"
134130
}
135131
]
136132
},

docs/framework/svelte/installation.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ title: Installation
55

66
You can install Svelte Query via [NPM](https://npmjs.com).
77

8-
> v5 is currently available as a release-candidate. We don't anticipate any major API changes from here on out. We encourage you to try it out and report any issues you find.
9-
108
### NPM
119

1210
```bash

docs/framework/svelte/overview.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ Then call any function (e.g. createQuery) from any component:
2828
<script lang="ts">
2929
import { createQuery } from '@tanstack/svelte-query'
3030
31-
const query = createQuery({
31+
const query = createQuery(() => ({
3232
queryKey: ['todos'],
3333
queryFn: () => fetchTodos(),
34-
})
34+
}))
3535
</script>
3636
3737
<div>
38-
{#if $query.isLoading}
38+
{#if query.isLoading}
3939
<p>Loading...</p>
40-
{:else if $query.isError}
41-
<p>Error: {$query.error.message}</p>
42-
{:else if $query.isSuccess}
43-
{#each $query.data as todo}
40+
{:else if query.isError}
41+
<p>Error: {query.error.message}</p>
42+
{:else if query.isSuccess}
43+
{#each query.data as todo}
4444
<p>{todo.title}</p>
4545
{/each}
4646
{/if}
@@ -62,6 +62,8 @@ Svelte Query offers useful functions and components that will make managing serv
6262
- `useQueryClient`
6363
- `useIsFetching`
6464
- `useIsMutating`
65+
- `useMutationState`
66+
- `useIsRestoring`
6567
- `useHydrate`
6668
- `<QueryClientProvider>`
6769
- `<HydrationBoundary>`
@@ -70,5 +72,4 @@ Svelte Query offers useful functions and components that will make managing serv
7072

7173
Svelte Query offers an API similar to React Query, but there are some key differences to be mindful of.
7274

73-
- Many of the functions in Svelte Query return a Svelte store. To access values on these stores reactively, you need to prefix the store with a `$`. You can learn more about Svelte stores [here](https://learn.svelte.dev/tutorial/writable-stores).
74-
- If your query or mutation depends on variables, you must use a store for the options. You can read more about this [here](../reactivity).
75+
- The arguments to the `create*` functions must be wrapped in a function to preserve reactivity.

docs/framework/svelte/reactivity.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

docs/framework/svelte/ssr.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export async function load() {
5858
5959
export let data: PageData
6060
61-
const query = createQuery({
61+
const query = createQuery(() => ({
6262
queryKey: ['posts'],
6363
queryFn: getPosts,
6464
initialData: data.posts,
65-
})
65+
}))
6666
</script>
6767
```
6868

@@ -136,10 +136,10 @@ export async function load({ parent, fetch }) {
136136
import { createQuery } from '@tanstack/svelte-query'
137137
138138
// This data is cached by prefetchQuery in +page.ts so no fetch actually happens here
139-
const query = createQuery({
139+
const query = createQuery(() => ({
140140
queryKey: ['posts'],
141141
queryFn: async () => (await fetch('/api/posts')).json(),
142-
})
142+
}))
143143
</script>
144144
```
145145

examples/svelte/auto-refetching/src/routes/+page.svelte

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,22 @@
8888
<button onclick={() => clearMutation.mutate(undefined)}> Clear All </button>
8989
</div>
9090
{/if}
91-
{#if todos.isFetching}
92-
<div style="color:darkgreen; font-weight:700">
93-
'Background Updating...' : ' '
94-
</div>
95-
{/if}
91+
92+
<pre
93+
class={['updating-text', todos.isFetching && 'on']}
94+
style="font-weight:700">Background Updating...</pre>
9695

9796
<style>
9897
li {
9998
text-align: left;
10099
}
100+
101+
.updating-text {
102+
color: transparent;
103+
transition: all 0.3s ease;
104+
}
105+
.updating-text.on {
106+
color: green;
107+
transition: none;
108+
}
101109
</style>

examples/svelte/basic/src/lib/Posts.svelte

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
</article>
3939
{/each}
4040
</ul>
41-
{#if posts.isFetching}
42-
<div style="color:darkgreen; font-weight:700">
43-
Background Updating...
44-
</div>
45-
{/if}
41+
<pre
42+
class={['updating-text', posts.isFetching && 'on']}
43+
style="font-weight:700">Background Updating...</pre>
4644
{/if}
4745
</div>
4846
</div>
@@ -53,8 +51,16 @@
5351
}
5452
a {
5553
display: block;
56-
color: white;
5754
font-size: 1.5rem;
5855
margin-bottom: 1rem;
5956
}
57+
58+
.updating-text {
59+
color: transparent;
60+
transition: all 0.3s ease;
61+
}
62+
.updating-text.on {
63+
color: green;
64+
transition: none;
65+
}
6066
</style>

examples/svelte/load-more-infinite-scroll/src/lib/LoadMore.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@
6060
.card {
6161
background-color: #111;
6262
margin-bottom: 1rem;
63+
color: rgba(255, 255, 255, 0.87);
6364
}
6465
</style>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
let name = $state('')
1515
1616
const postTodo = async ({ name, notes }: Omit<Todo, 'id'>) => {
17-
console.info('postTodo', { name, notes })
1817
return new Promise((resolve, reject) => {
1918
setTimeout(
2019
() => {
@@ -31,7 +30,7 @@
3130
}
3231
const todo = { name, notes, id: id.value }
3332
id.value = id.value + 1
34-
list.value = [...list.value, todo]
33+
list.value.push(todo)
3534
resolve(todo)
3635
},
3736
queryTimeMin.value +

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<button
2828
onclick={() => {
29-
views.value = [...views.value, '']
29+
views.value.push('')
3030
}}
3131
>
3232
Add Filter List

0 commit comments

Comments
 (0)