hooks icon indicating copy to clipboard operation
hooks copied to clipboard

[useRequest] error时data的值没有改变

Open baixiaoyu2997 opened this issue 10 months ago • 2 comments

当第一次执行成功时,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

baixiaoyu2997 avatar Jan 20 '25 05:01 baixiaoyu2997

看文档是说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

fnlearner avatar Jan 26 '25 06:01 fnlearner

我遇到了相同的问题,导致请求失败场景,页面展示的是上次成功的值。感觉请求失败,需要重置data更合理。 发现是请求失败时,未设置data为undefined Image packages/hooks/src/useRequest/src/Fetch.ts

Damon0820 avatar Feb 06 '25 06:02 Damon0820

目前是这么设计的,v4 再考虑是否调整

crazylxr avatar Aug 17 '25 13:08 crazylxr