hooks
hooks copied to clipboard
[useRequest] error时data的值没有改变
当第一次执行成功时,data的值是正确的,但是如果,第二次请求参数不同导致的error,不会让data变为undefined, 而是上次成功的值,我不知道这是一个bug还是feat
const { data, run }=useRequest(getInfo,{
cacheKey:undefined
})
useEffect(() => {
run({
RequestAmount: 5000, // success
});
setTimeout(() => {
run({
RequestAmount: 50000, // error
});
}, 2000);
}, []);
console.log(data)
// first time: {data: {RequestAmount:5000}}
// second time: {data: {RequestAmount:5000}} same with first time
看文档是说If options.cacheKey is set, useRequest will cache the successful data . The next time the component is initialized, if there is cached data, we will return the cached data first, and then send a new request in background, which is the ability of SWR.会缓存成功时候的数据https://ahooks.js.org/hooks/use-request/cache#cache--swr
我遇到了相同的问题,导致请求失败场景,页面展示的是上次成功的值。感觉请求失败,需要重置data更合理。
发现是请求失败时,未设置data为undefined
packages/hooks/src/useRequest/src/Fetch.ts
目前是这么设计的,v4 再考虑是否调整