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]竞态取消机制与cache #2675

Open
baixiaoyu2997 opened this issue Nov 18, 2024 · 3 comments
Open

[useRequest]竞态取消机制与cache #2675

baixiaoyu2997 opened this issue Nov 18, 2024 · 3 comments

Comments

@baixiaoyu2997
Copy link

baixiaoyu2997 commented Nov 18, 2024

竞态取消并不是真的取消发送请求,那么既然请求已经发送,返回值应该可以被缓存

const { data, runAsync, loading, error } = useRequest(getBusinessEntityWalletList, {
    manual: true,
    cacheKey: '123',
    staleTime: 15000,
    onSuccess(data, params) {
      console.log('==========success', params)
    },
  })

  useEffect(() => {
    setTimeout(() => {
      const asyncFn = async () => {
        console.log('=========run')
        runAsync({ Page: 1, PageSize: 20 })
        runAsync({
          ...{ Page: 1, PageSize: 20 },
          Page: 2,
        })
      }

      asyncFn()
    }, 3000)
  }, [])

setCache的时机在onSuccess和onMutate时才触发,引用:

但是因为竞态取消机制导致onSuccess只执行一次, 也就是说没办法缓存第一个请求的返回值,这里是否可以改进一下?

@crazylxr
Copy link
Collaborator

crazylxr commented Dec 5, 2024

这个不好搞,data 只有一份,之前的存哪里呢

@baixiaoyu2997
Copy link
Author

baixiaoyu2997 commented Dec 5, 2024

这个不好搞,data 只有一份,之前的存哪里呢

在自定义cache的时候是有意义的,因为我可以根据不同的参数来保存数据到localStorage, 这样我在其他组件中如果也触发了相同参数的请求会命中缓存。

这并不算是一个bug, 只是一种建议,或许应该转移到Discussion, 如果你觉得不合理可以随时关闭该issue

@fnlearner
Copy link

可以自己写个单例用map存这数据

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