-
-
Notifications
You must be signed in to change notification settings - Fork 126
Closed
Labels
Description
Describe the bug
当 reload 失败时,不会清空 data
我查看了源码,是因为 data 是手动维护的(finalData),当调用 reload(run) 时,如果 const currentData = await service(lastData); 出错,此时并不会操作 finalData。
const { loading, run, runAsync, cancel } = useRequest(
async (lastData?: TData) => {
const currentData = await service(lastData);
if (!lastData) {
finalData.value = currentData;
} else {
finalData.value = {
...currentData,
list: [...lastData.list, ...currentData.list],
};
}
return currentData;
}我还查看了 ahooks 的源码,发现也一样没有操作,不知道这是不是预期中的行为?
const { loading, error, run, runAsync, cancel } = useRequest(
async (lastData?: TData) => {
const currentData = await service(lastData);
if (!lastData) {
setFinalData({
...currentData,
list: [...(currentData.list ?? [])],
});
} else {
setFinalData({
...currentData,
list: isScrollToTop
? [...currentData.list, ...(lastData.list ?? [])]
: [...(lastData.list ?? []), ...currentData.list],
});
}
return currentData;
},Steps to reproduce
示例代码如下:
<template>
<div>
<div style="height: 100px; display: flex; align-items: center; justify-content: center">isError: {{ isError }}</div>
<div
v-for="item in data?.list"
:key="item"
style="padding: 12px; border: 1px solid #f5f5f5; text-align: center"
>
{{ item }}
</div>
<div style="display: flex; gap: 16px">
<button @click="reloadSuccess">reload success</button>
<button @click="reloadError">reload error</button>
<button @click="clear">clear</button>
</div>
</div>
</template>
<script lang="ts" setup>
import { useInfiniteScroll } from "vue-hooks-plus";
const isRequestSuccess = ref(true);
const isError = ref(false);
const { data, mutate, reload } = useInfiniteScroll<{ list: number[] }>(
async () => {
if (isRequestSuccess.value) {
return { list: [1, 2, 3, 4, 5] };
} else {
throw new Error("reload 异常");
}
},
{
manual: true,
onSuccess: () => {
isError.value = false;
},
onError: () => {
isError.value = true;
},
},
);
function reloadSuccess() {
isRequestSuccess.value = true;
reload();
}
function reloadError() {
isRequestSuccess.value = false;
reload();
}
function clear() {
mutate({ list: [] });
}
</script>
System Info
- macos 15.4.1
- chrome 139.0.7258.67 (arm64)Used Package Manager
pnpm
Validations
- Follow our Code of Conduct.
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a VueHooks Plus issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- The provided reproduction is a minimal reproducible example of the bug.