From 3ea7b2a514e1e689185ab3c9de379a7276e1e311 Mon Sep 17 00:00:00 2001 From: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com> Date: Thu, 6 Jul 2023 17:47:56 +0200 Subject: [PATCH 1/4] Update reactivity docs Use writable/derived stores --- docs/svelte/reactivity.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/docs/svelte/reactivity.md b/docs/svelte/reactivity.md index c4c8a105f9..675e063b42 100644 --- a/docs/svelte/reactivity.md +++ b/docs/svelte/reactivity.md @@ -3,18 +3,18 @@ id: reactivity title: Reactivity --- -Svelte uses a compiler to build your code which optimises rendering. By default, variables will run once, unless they are referenced in your markup. To be able to react to changes in options you need to use [stores](https://svelte.dev/docs/svelte-store). +Svelte uses a compiler to build your code which optimises rendering. By default, components run once, unless they are referenced in your markup. To be able to react to changes in options you need to use [stores](https://svelte.dev/docs/svelte-store). -In the below example, the `refetchInterval` option is set from the variable `intervalMs`, which is edited by the input field. However, as the query is not told it should react to changes in `intervalMs`, `refetchInterval` will not change when the input value changes. +In the below example, the `refetchInterval` option is set from the variable `intervalMs`, which is bound to the input field. However, as the query is not able to react to changes in `intervalMs`, `refetchInterval` will not change when the input value changes. ```markdown - - + ``` -To solve this, create a store for the options and use it as input for the query. Update the options store when the value changes and the query will react to the change. +To solve this, we can convert `intervalMs` into a writable store. The query options can then be turned into a derived store, which will be passed into the function with true reactivity. ```markdown - - + ``` From a8e1dbde83f74ca44c2d5ea62a27c3f9832c39cc Mon Sep 17 00:00:00 2001 From: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com> Date: Thu, 6 Jul 2023 18:08:09 +0200 Subject: [PATCH 2/4] Add test for reactivity bug --- .../src/__tests__/CreateQuery.svelte | 11 ++-- .../src/__tests__/createQuery.test.ts | 63 ++++++++++++++++--- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/packages/svelte-query/src/__tests__/CreateQuery.svelte b/packages/svelte-query/src/__tests__/CreateQuery.svelte index c702c66814..383d2e74f4 100644 --- a/packages/svelte-query/src/__tests__/CreateQuery.svelte +++ b/packages/svelte-query/src/__tests__/CreateQuery.svelte @@ -1,15 +1,12 @@ {#if $query.isPending} @@ -17,7 +14,7 @@ {:else if $query.isError}

Error

{:else if $query.isSuccess} -

Success

+

{$query.data}

{/if}