Skip to content

Commit 90e10e5

Browse files
authored
fix: 🔧 use-request ready as parameter, dependent refresh does not update
fix: 🔧 use-request ready as parameter, dependent refresh does not update
2 parents a8a987b + 0744f55 commit 90e10e5

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

packages/hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-hooks-plus",
3-
"version": "1.5.4",
3+
"version": "1.5.5",
44
"description": "Vue hooks library",
55
"files": [
66
"dist",
Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<template>
22
<div style="margin-top: 16px;">
33
<div>
4-
<p>id:{{ id }}</p>
5-
<p>storeID:{{ store.id }}</p>
4+
<vhp-button type="button" @click="count++">count is {{ count }}</vhp-button>
5+
<div style="opacity: 0.6;"> count !==0 and count !==5 ready is true </div>
66
</div>
7+
<br>
78
<vhp-button @click="() => (id = 1)">Change ID = 1</vhp-button>
89
<vhp-button @click="() => (id = 2)" style="margin-left: 16px;">Change ID = 2</vhp-button>
910
<vhp-button @click="() => (store.id = 1)" style="margin-left: 16px;">
@@ -14,28 +15,77 @@
1415
</vhp-button>
1516
</div>
1617
<div style="margin-top: 16px;">Loading:{{ loading ? 'loading' : '' }}</div>
18+
1719
<div style="margin-top: 16px;"
18-
>Data Value: <span>{{ data }}</span>
20+
>Data Value:
21+
<div>
22+
<table>
23+
<thead>
24+
<th>Time</th>
25+
<th>Id</th>
26+
<th>StoreId</th>
27+
<th>Count</th>
28+
</thead>
29+
<tbody>
30+
<tr>
31+
<th>{{ data?.time }}</th>
32+
<th>{{ data?.id }}</th>
33+
<th>{{ data?.storeId }}</th>
34+
<th>{{ data?.count }}</th>
35+
</tr>
36+
</tbody>
37+
</table>
38+
</div>
1939
</div>
2040
</template>
2141

2242
<script lang="ts" setup>
23-
import { ref, reactive } from 'vue'
43+
import { ref, reactive, computed } from 'vue'
2444
2545
import { useRequest } from 'vue-hooks-plus'
2646
27-
function getUsername({ id, storeId }: { id: number; storeId: number }): Promise<string> {
47+
function getUsername({
48+
id,
49+
storeId,
50+
count,
51+
}: {
52+
id: number
53+
storeId: number
54+
count: number
55+
}): Promise<{
56+
time: number | string
57+
id: number | string
58+
storeId: number | string
59+
count: number | string
60+
}> {
2861
return new Promise(resolve => {
2962
setTimeout(() => {
30-
resolve(`${String(Date.now())}; \n Params id: ${id} \t; Params storeId: ${storeId}`)
63+
resolve({
64+
time: Date.now(),
65+
id,
66+
storeId,
67+
count,
68+
})
3169
}, 1000)
3270
})
3371
}
3472
const id = ref(1)
3573
const store = reactive({
3674
id: 1,
3775
})
38-
const { data, loading } = useRequest(() => getUsername({ id: id.value, storeId: store.id }), {
39-
refreshDeps: [id, () => store.id],
40-
})
76+
const count = ref(0)
77+
const ready = computed(() => count.value !== 0 && count.value !== 5)
78+
const { data, loading } = useRequest(
79+
() => getUsername({ id: id.value, storeId: store.id, count: count.value }),
80+
{
81+
initialData: {
82+
time: Date.now(),
83+
id: '-',
84+
count: '-',
85+
storeId: '-',
86+
},
87+
ready,
88+
refreshDeps: [id, () => store.id, count],
89+
},
90+
)
4191
</script>

packages/hooks/src/useRequest/plugins/useAutoRunPlugin.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
import { isRef, unref, ref, watch } from 'vue'
1+
import { unref, ref, watch, watchEffect } from 'vue'
22
import { FetchState, Plugin } from '../types'
33

44
// support refreshDeps & ready
55
const useAutoRunPlugin: Plugin<unknown, unknown[]> = (
66
fetchInstance,
7-
{ manual, ready = true, defaultParams = [], refreshDeps = [], refreshDepsAction },
7+
{ manual, ready = true, refreshDeps = [], refreshDepsAction },
88
) => {
99
const hasAutoRun = ref(false)
10-
hasAutoRun.value = false
1110

12-
watch(isRef(ready) ? ready : [], r => {
13-
if (!manual && r) {
14-
hasAutoRun.value = true
15-
fetchInstance.run(...defaultParams)
16-
}
11+
watchEffect(() => {
12+
if (!manual) hasAutoRun.value = unref(ready)
1713
})
1814

1915
watch(
20-
refreshDeps,
21-
() => {
22-
if (hasAutoRun.value) return
23-
if (!manual) {
16+
[hasAutoRun, ...refreshDeps],
17+
([autoRun]) => {
18+
if (!autoRun) return
19+
if (!manual && autoRun) {
2420
if (refreshDepsAction) {
2521
refreshDepsAction()
2622
} else {
@@ -30,6 +26,7 @@ const useAutoRunPlugin: Plugin<unknown, unknown[]> = (
3026
},
3127
{
3228
deep: true,
29+
immediate: false,
3330
},
3431
)
3532

0 commit comments

Comments
 (0)