-
Notifications
You must be signed in to change notification settings - Fork 31
/
IsMutating.svelte
60 lines (52 loc) · 1.58 KB
/
IsMutating.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<script lang="ts">
import { IsMutating, useMutation } from '../../src'
import { useIsMutating } from '../../src/isMutating'
const later = (delay, value) =>
new Promise(resolve => setTimeout(resolve, delay, value))
// the mutation
const mutationFn = () => later(500, 'My response')
const useMutationResult = useMutation(mutationFn)
// the mutation 2
const mutationFn2 = () => later(500, 'My response 2')
const useMutationResult2 = useMutation(mutationFn2)
let isMutating = 0
let history = []
$: {
history = [...history, isMutating]
}
// useIsMutating
const isMutatingResult = useIsMutating()
let useHistory = []
$: {
useHistory = [...useHistory, $isMutatingResult]
}
</script>
<main>
<h3>IsFetching</h3>
<IsMutating bind:isMutating>
<div slot="isMutating">
isMutating change log:
<span>{JSON.stringify(history)}</span>
</div>
</IsMutating>
<h3>useIsMutating</h3>
<div>
useIsMutating change log:
{JSON.stringify(useHistory)}
<div>
<button
on:click={() => {
$useMutationResult.mutate()
$useMutationResult2.mutate()
}}>
Mutate All
</button>
<h3>Mutation 1</h3>
<button on:click={() => $useMutationResult.mutate()}>mutate</button>
{$useMutationResult.isLoading ? 'Mutation loading ...' : $useMutationResult.data || ''}
<h3>Mutation 2</h3>
<button on:click={() => $useMutationResult2.mutate()}>mutate 2</button>
{$useMutationResult2.isLoading ? 'Mutation 2 loading ...' : $useMutationResult2.data || ''}
</div>
</div>
</main>