Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
baixiaoyu2997 opened this issue Jan 20, 2025 · 2 comments
Open

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

baixiaoyu2997 opened this issue Jan 20, 2025 · 2 comments

Comments

@baixiaoyu2997
Copy link

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

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

@Damon0820
Copy link

Damon0820 commented Feb 6, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants